Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add custom keyboard shortcuts in Visual Studio Code?

I'd like to add in custom keyboard shortcuts - rather than just remapping existing key bindings. Is this possible?

The idea is to map a shortcut that will allow me to include an easily identifiable comment header to help index my projects - in this format:

/*--------------------------------------------------------------
# Header
--------------------------------------------------------------*/
like image 361
idk Avatar asked Nov 22 '25 20:11

idk


1 Answers

Here is a snippet (which is triggered by typing cHeader):

"Custom Header": {
  "prefix": ["cHeader"],
  "body": [
    "/*---------------------------------------------------------------",
      "# $1",
    "---------------------------------------------------------------*/"
    ]
}

You can make that to whatever length you want. For more complicated situations, see https://stackoverflow.com/a/56874352/836330 or https://stackoverflow.com/a/58722958/836330.

If you want to set a keybinding to that, use this:

{
  "key": "ctrl+alt+r",            // whatever you want as a keybinding
  "command": "editor.action.insertSnippet",
  "args": {
    "name": "Custom Header"      // name from your snippet above
  },
  "when": "editorTextFocus"
}
like image 165
Mark Avatar answered Nov 25 '25 09:11

Mark