Sublime Forum

How to enclose selection in curly brackets when typing "$"?

#1

In JavaScript one can use variables in string templates: `This is a ${rating} string`. I enjoyed the functionality that, when I selected a part of a backtick-enclosed string and typed the dollar sign, ST automatically enclosed the selected string in curly brackets and placed the dollar sign in front of it.
But now this doesn’t seem to work any more.
Can anyone tell me how I can bring this cool little feature back to live?

0 Likes

#2

as a local fix, add the following to your keybindings

    { "keys": ["$"], "command": "insert_snippet", "args": {"contents": "\\${${0:$SELECTION}}"}, "context":
        [
            { "key": "selector", "operator": "equal", "operand": "string.quoted.other.js | string.template.js", "match_all": true },
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
        ]
    },
1 Like

#3

The above keybinding for some reason does not work in my ST3 inside backtick-strings. The selected text is always being replaced with the dollar sign.
But thank you anyway for your efforts and esp. thank you for linking to the pull request. So I guess I can wait for the next release of the JS package.

0 Likes

#4

What’s the output when you press ctrl+alt+shift+p in a backtick-string?

0 Likes

#5

In a *.vue file where I tried it it is

text.html.vue
source.ts.embedded.html
source.ts meta.export.default.ts
meta.class.ts meta.method.declaration.ts
meta.block.ts meta.block.ts
meta.block.ts
new.expr.ts
string.template.ts

and in a *.js file where I tried it the context is

source.js
string.interpolated.js

0 Likes

#6

you can use

{ "keys": ["$"], "command": "insert_snippet", "args": {"contents": "\\${${0:$SELECTION}}"}, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "string.quoted.other.js | string.interpolated.js | string.template.ts", "match_all": true },
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
    ]
},
1 Like

#7

You are right - sorry for my lack of knowledge, I could’ve come up with that adjustment myself. Now it works like a charm, thank you so much!

1 Like