How to use Visual Studio Code as Default Editor for Git
In the most recent release (v1.0, released in March 2016), you are now able to use VS Code as the default git commit/diff tool. Quoted from the documentations:
Make sure you can run
code --help
from the command line and you get help.
if you do not see help, please follow these steps:
Mac: Select Shell Command: Install 'Code' command in path from the Command Palette.
- Command Palette is what pops up when you press shift + ⌘ + P while inside VS Code. (shift + ctrl + P in Windows)
- Windows: Make sure you selected Add to PATH during the installation.
- Linux: Make sure you installed Code via our new .deb or .rpm packages.
- From the command line, run
git config --global core.editor "code --wait"
Now you can run
git config --global -e
and use VS Code as editor for configuring Git. Add the following to enable support for using VS Code as diff tool:
[diff] tool = default-difftool[difftool "default-difftool"] cmd = code --wait --diff $LOCAL $REMOTE
This leverages the new
--diff
option you can pass to VS Code to compare two files side by side.To summarize, here are some examples of where you can use Git with VS Code:
git rebase HEAD~3 -i
allows to interactive rebase using VS Codegit commit
allows to use VS Code for the commit messagegit add -p
followed bye
for interactive addgit difftool <commit>^ <commit>
allows to use VS Code as diff editor for changes
For what I understand, VSCode is not in AppData anymore.
So Set the default git editor by executing that command in a command prompt window:
git config --global core.editor "'C:\Program Files (x86)\Microsoft VS Code\code.exe' -w"
The parameter -w
, --wait
is to wait for window to be closed before returning. Visual Studio Code is base on Atom Editor. if you also have atom installed execute the command atom --help
. You will see the last argument in the help is wait.
Next time you do a git rebase -i HEAD~3
it will popup Visual Studio Code. Once VSCode is close then Git will take back the lead.
Note: My current version of VSCode is 0.9.2
I hope that help.
Another useful option is to set EDITOR
environment variable. This environment variable is used by many utilities to know what editor to use. Git also uses it if no core.editor
is set.
You can set it for current session using:
export EDITOR="code --wait"
This way not only git
, but many other applications will use VS Code as an editor.
To make this change permanent, add this to your ~/.profile
for example. See this question for more options.
Another advantage of this approach is that you can set different editors for different cases:
- When you working from local terminal.
- When you are connected through SSH session.
This is useful especially with VS Code (or any other GUI editor) because it just doesn't work without GUI.
On Linux OS, put this into your ~/.profile
:
# Preferred editor for local and remote sessionsif [[ -n $SSH_CONNECTION ]]; then # SSH mode export EDITOR='vim'else # Local terminal mode export EDITOR='code -w'fi
This way when you use a local terminal, the $SSH_CONNECTION
environment variable will be empty, so the code -w
editor will be used, but when you are connected through SSH, then $SSH_CONNECTION
environment variable will be a non-empty string, so the vim
editor will be used. It is console editor, so it will work even when you are connected through SSH.