5

Suppose following code exists.

sample text

When user double click text, then press { or (, it just wraps the text while keeping it.

sample {text}
sample (text)

But I don't know how to apply this rule to $ in VS Code Settings. What I expect is

sample $text$

Which setting in VS Code is related to this feature?

Mark
  • 143,421
  • 24
  • 428
  • 436
lee
  • 185
  • 2
  • 8

1 Answers1

6

Edit> Auto Surround

is the setting in vscode. But it only applies to quotes and brackets like (), {}, <> and [] (and possibly some other language-defined cases). You cannot change that setting to include another character like $ unfortunately.

Here is a keybinding you might try (in keybindings.json):

{
  "key": "alt+4",         // or whatever keybinding you wish
  "command":  "editor.action.insertSnippet",
  "args": {
    //  "snippet": "\\$$TM_SELECTED_TEXT\\$"

       // to have the text still selected after the $'s are inserted, use this
    "snippet": "\\$${1:$TM_SELECTED_TEXT}\\$"
  },
  "when": "editorTextFocus && editorHasSelection"
},

So that any selected text will be wrapped by a $ when you select it and alt+4 (where the $ is on an English keyboard). If you do that operation a lot it might be worth it.

If you use this line instead in the snippet above:

    "snippet": "$1$TM_SELECTED_TEXT$1"  // or

    "snippet": "$1${2:$TM_SELECTED_TEXT}$1"

then more generically select text to surround, trigger that keybinding and type whichever and how many characters you want to wrap the selection.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • It really helped. Thanks! BTW, it loses focus after the command, but there seems no option to keep the focus on selected text. Is there any related one? – lee Mar 09 '20 at 03:57
  • 1
    I edited my answer so that the selected text remains selected after being wrapped. On the second generic version remember to hit `tab` after you type the wrapping - then the original selected text will be selected again. – Mark Mar 09 '20 at 05:51