Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 40 ยท Customization and Productivity

Keyboard Shortcuts and Keybindings

Learn to view, change and create VS Code keyboard shortcuts with the Keyboard Shortcuts editor and keybindings.json, including when clauses, chords and arguments.

The keyboard is the fastest way to use an editor. VS Code ships with a rich set of default shortcuts, and every one of them can be changed. You can also bind keys to commands that have no shortcut, give a command arguments, or make a shortcut work only in some situations. This lesson shows the Keyboard Shortcuts editor, the JSON file behind it and the condition language called when clauses.

The runnable parts are JavaScript stand-ins that model how a key press is matched against a list of bindings; real keybinding resolution happens inside the editor.

Finding shortcuts

Open File > Preferences > Keyboard Shortcuts (Ctrl+K Ctrl+S). The editor lists every command with its key, its when condition and its source (Default or User). You can:

  • Search by command name or by key: click the keyboard icon in the search box and press the keys to find who uses them.
  • Right-click a row for Change Keybinding, Remove Keybinding, Reset Keybinding and Copy Command ID.
  • Double-click the Keybinding column to record a new key. Press the keys; for a chord, press the second key combination after the first, then Enter.
  • Spot conflicts: the editor warns when a key is already used and the when conditions overlap.

The Command Palette (Ctrl+Shift+P) also shows each command's current key, which is a good way to learn shortcuts you use through menus.

A cheat sheet: Help > Keyboard Shortcut Reference opens a PDF for your platform.

keybindings.json

Click the file icon at the top right of the Keyboard Shortcuts editor (or run Preferences: Open Keyboard Shortcuts (JSON)) to edit keybindings.json. It is a list of objects:

JSON
[
  {
    "key": "ctrl+shift+d",
    "command": "editor.action.copyLinesDownAction",
    "when": "editorTextFocus && !editorReadonly"
  },
  {
    "key": "ctrl+alt+t",
    "command": "workbench.action.terminal.toggleTerminal"
  },
  {
    "key": "ctrl+k ctrl+w",
    "command": "workbench.action.closeAllEditors"
  }
]
Field Meaning
key The shortcut, lowercase, modifiers joined by + (ctrl, shift, alt, meta/cmd/win)
command The command ID (copy it from the editor)
when Optional condition: the binding is active only when it is true
args Optional arguments for the command

A chord is two key combinations separated by a space, such as ctrl+k ctrl+w. After the first combination, the status bar shows that VS Code is waiting for the second.

Removing and overriding

To disable a default shortcut, repeat its command with a minus sign in front:

JSON
{ "key": "ctrl+shift+k", "command": "-editor.action.deleteLines" }

Later entries win over earlier ones, and user entries win over defaults. To move a binding, remove the old one and add the new one.

when clauses

A when clause is a boolean expression over context keys. Some common keys:

Context key True when
editorTextFocus The cursor is in a text editor
editorHasSelection Text is selected
editorLangId == python The file's language is Python
terminalFocus The terminal has focus
explorerViewletVisible The Explorer view is visible
inDebugMode A debug session is running
suggestWidgetVisible The completion list is open
resourceExtname == .md The file has the .md extension

Operators: && (and), || (or), ! (not), ==, !=, =~ (regex match) and parentheses. Use Developer: Inspect Context Keys from the Command Palette to see the keys' values at a particular moment.

JSON
{
  "key": "ctrl+enter",
  "command": "workbench.action.terminal.runSelectedText",
  "when": "editorTextFocus && editorHasSelection && editorLangId == shellscript"
}

Commands with arguments

Some commands accept args. For example, to insert a snippet with a keyboard shortcut (see snippets) or to type fixed text:

JSON
{
  "key": "ctrl+alt+l",
  "command": "editor.action.insertSnippet",
  "args": { "snippet": "console.log('${1:label}', ${2:value});$0" },
  "when": "editorTextFocus && editorLangId == javascript"
}

Another common one sends characters to the terminal:

JSON
{
  "key": "ctrl+alt+c",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "clear\u000D" },
  "when": "terminalFocus"
}

\u000D is the Enter key, so the command runs immediately.

Keymap extensions

If you come from another editor, install a keymap extension such as Vim, Sublime Text Keymap, IntelliJ IDEA Keybindings or Atom Keymap. They remap defaults so your muscle memory keeps working. The Vim extension is a full modal emulation with its own settings (vim.*), not only a keymap.

Habits for effective shortcuts

  1. Learn about ten shortcuts well before customizing: Command Palette, Quick Open, Go to Symbol, Multi-cursor, Move Line, Toggle Terminal, Toggle Sidebar, Find in Files, Go to Definition, Rename.
  2. Customize only what you press all day.
  3. Prefer a chord (ctrl+k prefix) for rarely used commands, to avoid collisions.
  4. Keep keybindings.json in a settings profile or a dotfiles repository so you can restore it.

Runnable stand-ins

JS Stand-in: match a key press to a binding

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: detect conflicting shortcuts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which shortcut opens the Keyboard Shortcuts editor?

    Ctrl+K Ctrl+S is a chord that opens Keyboard Shortcuts. Ctrl+, opens Settings.
  2. 2. How do you remove a default keybinding in keybindings.json?

    An entry like "-editor.action.deleteLines" removes the matching default binding.
  3. 3. What is a when clause?

    when clauses use context keys such as editorTextFocus to limit where a shortcut works.
  4. 4. How is a chord written in keybindings.json?

    A chord is a space-separated pair of key combinations.

Summary

  • Search and change shortcuts in the Keyboard Shortcuts editor; edit keybindings.json for full control.
  • Each binding has key, command, optional when and args.
  • Prefix a command with - to remove a default binding.
  • Use context keys in when clauses and inspect them with Developer: Inspect Context Keys.
  • Keymap extensions bring shortcuts from other editors.

Next: Snippets