Keybindings, snippets and profiles

VS Code · Advanced · 12 min

Tailoring shortcuts and boilerplate to your workflow, and keeping it in sync across machines, is where heavy users gain most.

Keybindings. The Keyboard Shortcuts editor (Ctrl+K Ctrl+S) edits keybindings.json, an array of rules with key, command, optional args and a when clause: a context expression such as editorTextFocus, terminalFocus or editorLangId == 'markdown'. The same key can do different things in different contexts. Prefix a command with - to remove a default binding. "Developer: Toggle Keyboard Shortcuts Troubleshooting" logs which rule fired for each key press.

Snippets. "Snippets: Configure Snippets" creates global or per-language snippet files. Each snippet has a prefix (what you type), a body and an optional description. In the body:

  • $1, $2 are tab stops, $0 the final cursor position.
  • ${1:default} is a placeholder; repeating $1 mirrors the text.
  • Variables like $TM_FILENAME_BASE, $CURRENT_YEAR and $CLIPBOARD insert context.

Project snippets can live in .vscode/*.code-snippets and are shared via the repo.

Profiles bundle settings, extensions, keybindings, snippets and UI state. Create one per context (say, "Python data", "Writing", "Presentation" with big fonts) and switch instantly; a folder can remember its profile. Settings Sync (sign in with GitHub or Microsoft) keeps profiles and settings identical across your machines.

Example

// keybindings.json
[
  { "key": "ctrl+alt+t", "command": "workbench.action.terminal.new" },
  { "key": "ctrl+enter", "command": "markdown.showPreviewToSide",
    "when": "editorTextFocus && editorLangId == 'markdown'" },
  { "key": "ctrl+shift+k", "command": "-editor.action.deleteLines" }
]
// typescript.json (user snippets)
{
  "Log variable": {
    "prefix": "logv",
    "body": ["console.log('${1:name}:', $1);$0"],
    "description": "Log a labelled variable"
  }
}

Remember

  • keybindings.json rules: key, command, when; -command removes a default
  • Snippet bodies use $1 tab stops, ${1:placeholder}, $0 and variables
  • Profiles switch whole setups; Settings Sync shares them across machines

Watch out for

  • Adding a new binding for a key already used in that context without a when clause, silently breaking a default.

Check yourself

  1. In a snippet body, what does writing `$1` twice do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: code.visualstudio.com/docs/configure/keybindings