Search code examples
visual-studio-codecode-snippets

VS Code : create custom snippet/shortcut


I would like to have the following feature on VSCode and I don't know if it is possible. For example, if I have this line :

I would like to emphasize this.

I would just select this, clic on a shortcut like ctrl+i and then this appears :

I would like to emphasize {i}this{/i}.

I use a lot of {i} and {/i} tags in my project so this would help me save an incredible amount of time !

I know VSCode already does something similar when you select a word and clic on "


Solution

  • Find your keybindings.json file and insert the following snippet:

    {
      "key": "ctrl+i",
      "command": "editor.action.insertSnippet",
      "args": {
        "snippet": "{i}$TM_SELECTED_TEXT{/i}"
      },
      "when": "editorTextFocus && editorHasSelection"
    }
    

    Key bindings can be found by pressing Ctrl+Shift+P > type "Keyboard shortcuts", full name being: Open Keyboard Shortcuts (JSON).