Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 308
elfxiong Avatar asked Aug 16 '16 22:08

elfxiong


People also ask

How do I add a keyboard shortcut to VSCode?

All keyboard shortcuts in VS Code can be customized via the keybindings. json file. To configure keyboard shortcuts through the JSON file, open Keyboard Shortcuts editor and select the Open Keyboard Shortcuts (JSON) button on the right of the editor title bar. This will open your keybindings.

How do you edit multiple lines at once in VSCode?

Select the lines you want and then press: Windows: Shift + Alt + i. Mac: shift + option + i.

How do you jump to a specific line in VSCode?

Navigate to a Specific Line Note: To go to a line in the file, you use ctrl + g, then type a line number. Alternatively, you can also open the go-to file menu with command + p first.


2 Answers

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"
}
like image 115
opus131 Avatar answered Sep 24 '22 15:09

opus131


The accepted answer didn't work for me so I raised a ticket on GitHub:

When condition for Source Control View for better keybindings #99683

The following did:

{
  "key": "ctrl+a",
  "command": "git.stageAll",
  "when": "sideBarFocus && activeViewlet == 'workbench.view.scm'"
},
{
  "key": "ctrl+s",
  "command": "git.stage",
  "when": "sideBarFocus && activeViewlet == 'workbench.view.scm'"
},
{
  "key": "ctrl+u",
  "command": "git.unstage",
  "when": "sideBarFocus && activeViewlet == 'workbench.view.scm'"
},

UPDATE:

I was also able to get single keys working via the addition of the listFocus attribute:

{
  "key": "s",
  "command": "git.stage",
  "when": "listFocus && sideBarFocus && activeViewlet == 'workbench.view.scm'"
},
like image 31
rndware Avatar answered Sep 24 '22 15:09

rndware