在 vscode 中设置字符 $ 将文本换行为括号

lee*_*lee 6 visual-studio-code vscode-snippets

假设存在以下代码。

sample text
Run Code Online (Sandbox Code Playgroud)

当用户双击text,然后按{或 时(,它只会在保留文本的同时换行。

sample {text}
sample (text)
Run Code Online (Sandbox Code Playgroud)

但我不知道如何在 VS Code 设置中应用此规则$。我期望的是

sample $text$
Run Code Online (Sandbox Code Playgroud)

VS Code 中的哪个设置与此功能相关?

Mar*_*ark 6

编辑>自动环绕声

是vscode中的设置。但它仅适用于引号和括号(), {}, <> and [](可能还有其他一些语言定义的情况)。不幸的是,您无法更改该设置以包含另一个角色$

这是您可以尝试的键绑定(在 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"
},
Run Code Online (Sandbox Code Playgroud)

这样,$当您选择任何选定的文本时,它都会被 a 和alt+包围4(其中$位于英文键盘上)。如果您经常进行该操作,那么可能是值得的。

如果您在上面的代码片段中使用这一行:

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

    "snippet": "$1${2:$TM_SELECTED_TEXT}$1"
Run Code Online (Sandbox Code Playgroud)

然后更一般地选择要包围的文本,触发该键绑定并键入您想要包围所选内容的字符和数量。