June 5, 2018

VS Code: binding the same keys for next/prev change everywhere

In current VS Code, there are different actions for navigating changes:

  • workbench.action.editor.nextChange for going to next change in an editor
  • workbench.action.compareEditor.nextChange for going to next change in a diff viewer
  • editor.action.dirtydiff.next for viewing some sort of local diff in the editor
Plus of course equivalents for "previous change".

Now, I don't particularly care about the last one with a preview window. However, I want to move in the editor and in the diff viewer with the same keys.

This is not easy to do, because of VS Code's binding resolution order, and the built-in keybinding editor will not allow you to set it up right.

Here's a snippet of keybindings.json that does what I want. Binds Alt+J to "next change" and Alt+K to "previous change", both in diff and in regular editor. The key here is to set up two distinct when conditions.


    {
        "key": "alt+j",
        "command": "workbench.action.editor.nextChange",
        "when": "editorTextFocus"
    },
    {
        "key": "alt+k",
        "command": "workbench.action.editor.previousChange",
        "when": "editorTextFocus"
    },
    {
        "key": "alt+k",
        "command": "workbench.action.compareEditor.previousChange",
        "when": "isInDiffEditor",
    },
    {
        "key": "alt+j",
        "command": "workbench.action.compareEditor.nextChange",
        "when": "isInDiffEditor",
    },

No comments: