How do I make a shortcut in VS Code to force git
to add a file? EG I keep *.png
in .gitignore
so image files are ignored, but sometimes I want to add an image file so I can include it in README.md
. I can do that EG with git add -f ./dir_name/image.png
. Is it possible to make this into a keyboard shortcut in VS Code?
Add this to your keybindings.json
:
{
"key": "shift+alt+a",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "git add -f ${relativeFile}u000D"
}
},
Explanation: when you press "shift+alt+a"
, what follows after "text"
in "args"
is sent to the terminal. VS Code will replace ${relativeFile}
with the relative path to the currently active file. u000D
is the code for a carriage return (“ENTER key”), which effectively “presses enter” after sending the text to the terminal. Without u000D
, the command will be sent to the terminal but will not run, instead it will wait for you to focus the terminal and press enter.
Sources: 1 2 3 4 5 6 7