Search code examples
gitvisual-studio-codegit-stage

VSCode how to add keyboard shortcut for staging selected lines


There is a Stage Selected Lines button for Git, but is there a way to add a keyboard shortcut for it?

stage selected lines option

It is too much trouble having to click twice (three dots > Stage Selected Lines).


Solution

  • In case anyone is still looking for this:

    The commands stageSelectedRanges, unstageSelectedRanges, revertSelectedRanges have been introduced a while ago. Note that there is currently a related bug in VSCode: the diff view does not refresh after a partial stage - but apart from that it works like a charm.

    This is how I set it up in my keybindings:

    {
      "key": "s",
      "command": "git.stageSelectedRanges",
      "when": "isInDiffEditor && editorTextFocus"
    },
    {
      "key": "u",
      "command": "git.unstageSelectedRanges",
      "when": "isInDiffEditor && editorTextFocus"
    },
    {
      "key": "r",
      "command": "git.revertSelectedRanges",
      "when": "isInDiffEditor && editorTextFocus"
    }