VS Code - 键绑定 - *type* 命令的光标位置

Mar*_*vid 1 keymapping key-bindings cursor-position visual-studio-code vscode-snippets

我使用的是 VS Code 版本:1.40.0。

为了加快我的开发速度,我需要设置自己的键绑定以在代码中输入特定文本(“ {|print_x} ”)。我设法做到了这一点,但如果我粘贴文本后类型光标立即跳到“ { ”之后,那就更好了。

所以:{ 这里输入光标 |print_x}

keybindings.json中的代码:

 { 
    "key": "shift+alt+y", 
    "command": "type",
    "args": { "text": "{|print_x}", },
    "when": "editorTextFocus" 
}
Run Code Online (Sandbox Code Playgroud)

我认为使用这样的数组可能会起作用,但不幸的是文本参数需要是字符串。

   "args": { "text": [ "{" , "|print_x}" ], }
Run Code Online (Sandbox Code Playgroud)

有办法做到吗?如果是这样,我将非常感激。

Mar*_*ark 6

只需使用此表格即可:

 { 
    "key": "shift+alt+y",
    "command":  "editor.action.insertSnippet",
    "args": {
      "snippet": "{$1print_x}"
    },
    "when": "editorTextFocus" 
  }
Run Code Online (Sandbox Code Playgroud)

因为这使用了insertSnippet命令,所以您现在可以在键绑定中直接使用制表符或变量转换,而无需单独的代码片段。这样光标就会移动到他所在的位置$1所在的位置。

insertSnippet可以执行该type命令的操作并为您提供制表符。