How to use Visual Studio Code as default editor for git?
When using git at the command line, I am wondering if it is possible to use Visual Studio Code as the default editor, i.e. when creating commit comments and looking at a diff of a file from the command line.
I understand that it won't be possible to use it for doing merges (at least at the minute) but does anyone know if it is possible to use it for looking at diff's, and if so, what command line options would be required in the .gitconfig file to make this happen?
UPDATE 1:
I have tried an approach similar to what I have done for Notepad++ in the past, i.e.
#!/bin/sh
"c:/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -notabbar -nosession -noPlugin "$*"
And used:
#!/bin/sh
"C:\Users\gep13\AppData\Local\Code\app-0.1.0\Code.exe" "$*"
But this results in an error message:
C:\temp\testrepo [master +1 ~0 -0]> git commit
[8660:0504/084217:ERROR:crash_reporter_win.cc(70)] Cannot initialize out-of-process crash handler
Aborting commit due to empty commit message.
C:\temp\testrepo [master +1 ~0 -0]>
Code opens up correctly, with the expected content, but it isn't waiting on the response, i.e. clicking save and closing the window to return to prompt.
UPDATE 2:
I have just heard back from one of the developers working on VSCode. Looks like this functionality currently isn't supported :-(
https://twitter.com/IsidorN/status/595501573880553472
If you are interested in seeing this feature get added, you might want to think about adding your votes here:
http://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/7756482-support-git-configure-diff-and-merge-tools
UPDATE 3:
I have been reliably informed that this feature has been picked up by the VSCode team, so I am looking forward to a future release that will include it.
UPDATE 4:
Thanks to @f-boucheros comment below, I have been able to get VS Code working as the default editor for commit comments, rebase, etc. I would still like to see if it is possible to use it as the diff tool as well.
UPDATE 5:
As per the accepted answer for the question, this is now possible using the V1.0 release of code.
Solution 1:
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
Solution 2:
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.
Solution 3:
Another useful option is to set EDITOR
and VISUAL
environment variables. These environment variables are used by many applications and utilities to know what editor to use. Git also uses one of them (depending on Git version) if no core.editor
is set.
You can set it for current session using:
export EDITOR="code --wait"
export VISUAL="$EDITOR"
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 sessions
if [[ -n $SSH_CONNECTION ]]; then # SSH mode
export EDITOR='vim'
else # Local terminal mode
export EDITOR='code -w'
fi
export VISUAL="$EDITOR"
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.
For the difference between EDITOR
and VISUAL
environment variables please see this question.
Solution 4:
You need to use command:
git config --global core.editor "'C:\Program Files\Microsoft VS Code\code.exe' -n -w"
Make sure you can start your editor from Git Bash
If you want to use Code.exe with short path, you can do this by adding the following line to your .bash_profile:
alias vscode="C:/Program\ Files/Microsoft\ VS\ Code/Code.exe"
And now, you can call it using only vscode
command(or whatever you named it)
Some additional info:
Setup will add Visual Studio Code to your %PATH%, so from the console you can type 'code' to open VS Code on that folder. You will need to restart your console after the installation for the change to the %PATH% environmental variable to take effect.
Solution 5:
git config --global core.editor "code --wait"
OR
git config --global core.editor "code -w"
Check
git config --global e
Your configuration will open in Visual Studio Code