2

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?

4

1 回答 1

4

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.

于 2020-03-09T02:23:53.350 回答