Search code examples
visual-studio-codekey-bindingscursor-positionkeymappingvscode-snippets

VS Code - Key bindings - cursor position for *type* command


I am using VS Code Version: 1.40.0.

for quicken up my development I would need to set my own keybinding for entering a certain text ("{|print_x}") into the code. I managed to do that, but even better would be if the type cursor would jump right after "{" as soon as I'd paste the text.

So: { here type curosor |print_x}.

Code in keybindings.json:

 { 
    "key": "shift+alt+y", 
    "command": "type",
    "args": { "text": "{|print_x}", },
    "when": "editorTextFocus" 
}

I thought using an array like that might work, but unfortunately text argument needs to be string.

   "args": { "text": [ "{" , "|print_x}" ], }

Is there a way to do it? If so, I would be very thankful.


Solution

  • Just use this form instead:

     { 
        "key": "shift+alt+y",
        "command":  "editor.action.insertSnippet",
        "args": {
          "snippet": "{$1print_x}"
        },
        "when": "editorTextFocus" 
      }
    

    Because this uses the insertSnippet command, you can now use tabstops or variable transforms right in a keybinding without a separate snippet. So the cursor will go to where he $1 is.

    insertSnippet can do what the type command does and gives you tabstops.