Reputation: 19021
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
Output:
[8660:0504/084217:ERROR:crash_reporter_win.cc(70)] Cannot initialize out-of-process crash handler
Aborting commit due to empty commit message.
Visual Studio 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 the prompt.
UPDATE 2:
I have just heard back from one of the developers working on Visual Studio Code. It looks like this functionality currently isn't supported :-(
From a tweet:
"@gep13 @NathanGloyn @code I checked and what you want to do is currently not possible, unfortunately."
If you are interested in seeing this feature get added, you might want to think about adding your votes here:
Support Git configure diff and merge tools
UPDATE 3:
I have been reliably informed that this feature has been picked up by the Visual Studio Code 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 Visual Studio 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 version 1.0 release of Visual Studio Code.
Upvotes: 708
Views: 474372
Reputation: 5429
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:
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.
Upvotes: 41
Reputation: 15492
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 you to interactively rebase using VS Codegit commit
allows you to use VS Code for the commit messagegit add -p
followed bye
for interactive addgit difftool <commit>^ <commit>
allows you to use VS Code as diff editor for changes
Upvotes: 1190
Reputation: 4183
Just another way to use Visual Studio Code as editor for Git if you can´t change your Git configuration or if you have to set EDITOR
to another editor for some reason.
export GIT_EDITOR="code --wait"
Upvotes: 0
Reputation: 181
I'm adding an answer because all the others were not succinct enough for my liking/did not fit my exact situation. I'm running a MacBook Air 2021 with an M1 chip
To make VS Code you default git editor in VS Code:
Step 1
Run the command git config --global core.editor "code --wait"
in your command line.
Step 2
If you try making a commit at this stage, you might get an error that looks like this:
"hint: Waiting for your editor to close the file... code -w: code: command not found error: There was a problem with the editor 'code -w'".
This means you don't have the shell command "code" installed in VS Code. Use (command + shift + p) to open the command centre in VS Code. Search for "code" and select "Shell Command: Install ‘code’ command in PATH". This adds "code" to your path.
Step 3
While trying to install "code", you might get an error that looks like this:
"EACCES: permission denied, unlink '/usr/local/bin/code'".
I don't know what causes this but a simple uninstall and re-install resolves it. To uninstall, search "uninstall" in the VS Code command centre and select "Shell Command: Uninstall ‘code’ command in PATH"
Step 4
Run the command git config --global -e
to test what your new default editor is. Your config file should open in VS Code text editor
Sources:
VS Code Denied Permission unlink 'usr/local/bin/code'
Upvotes: 18
Reputation: 5101
In 2022 on Windows 10 you should have next path:
git config --global core.editor "'C:\Users\<your username here>\AppData\Local\Programs\Microsoft VS Code\code.exe' -w"
Upvotes: 2
Reputation: 1593
git config --global core.editor "code --wait"
or
git config --global core.editor "code -w"
verify with:
git config --global -e
Your configuration will open in Visual Studio Code.
Upvotes: 135
Reputation: 71
I added git bash
in vscode terminal
as default
If you are lazy like me. Then I advise you to do the same as me.
Step1: Open setting: Ctrl + ,
Step2: search .json
--> choose file setting.json
Step3: Paste the code at the end of the file but in brackets
'}'
"terminal.integrated.defaultProfile.windows": "gitbash", "terminal.integrated.shell.windows":"E:\app\git\Git\bin\bash.exe",
Upvotes: 3
Reputation: 2187
what command line options would be required in the .gitconfig file to make this happen?
I had to add this to my .gitconfig file for git-lense to work:
[core]
# Make sure that interactive rebases open correctly with vs code
editor = code -w -n
-n
is optional – as Alex mentioned below it's short for --new-window
and I find it helpful :)
Upvotes: 2
Reputation: 1328002
In addition of export EDITOR="code --wait"
, note that, with VSCode v1.47 (June 2020), those diff editors will survice a VSCode reload/restart.
See issue 99290:
with commit 1428d44, diff editors now have a chance to survive reloads and this works OK unless the diff editor on a git resource is opened as the active one:
(and commit 24f1b69 fixes that)
Upvotes: 3
Reputation: 11
Just want to add these back slashes to previous answers, I am on Windows 10 CMD, and it doesn't work without back slashes before the spaces.
git config --global core.editor "C:\\Users\\your_user_name\\AppData\\Local\\Programs\\Microsoft\ VS\ Code\\Code.exe"
Upvotes: 1
Reputation: 409
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.
Upvotes: 20
Reputation: 683
on windows 10 using the 64bit insiders edition the command should be:
git config --global core.editor "'C:\Program Files\Microsoft VS Code Insiders\bin\code-insiders.cmd'"
you can also rename the 'code-insiders.cmd' to 'code.cmd' in the 'Program Files' directory, in this way you can now use the command 'code .' to start editing the files on the . directory
Upvotes: 0
Reputation: 1504
Run this command in your Mac Terminal app
git config --global core.editor "/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
Upvotes: 4
Reputation: 3950
Good news! At the time of writing, this feature has already been implemented in the 0.10.12-insiders release and carried out through 0.10.14-insiders. Hence we are going to have it in the upcoming version 1.0 Release of VS Code.
Implementation Ref: Implement -w/--wait command line arg
Upvotes: 3
Reputation: 1864
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.
Upvotes: 55
Reputation: 1
I set up Visual Studio Code as a default to open .txt file. And next I did use simple command: git config --global core.editor "'C:\Users\UserName\AppData\Local\Code\app-0.7.10\Code.exe\'"
. And everything works pretty well.
Upvotes: 0
Reputation: 7365
GitPad sets your current text editor as the default editor for Git.
My default editor for .txt
files in Windows 10 is Visual Studio Code and running GitPad once made it the default editor for Git. I haven't experienced the problems mentioned in the question (Git waits until VS Code window is closed in my case).
(The link for the .exe
file didn't work for me, you may need to compile the source yourself.)
Upvotes: 1
Reputation: 3664
I opened up my .gitconfig
and amended it with:
[core]
editor = 'C:/Users/miqid/AppData/Local/Code/app-0.1.0/Code.exe'
That did it for me (I'm on Windows 8).
However, I noticed that after I tried an arbitrary git commit
that in my Git Bash console I see the following message:
[9168:0504/160114:INFO:renderer_main.cc(212)] Renderer process started
Unsure of what the ramifications of this might be.
Upvotes: 3
Reputation: 1607
Im not sure you can do this, however you can try these additions in your gitconfig file.
Try to replace the kdiff3 from these values to point to visual studio code executable.
[merge]
tool = kdiff3
[mergetool "kdiff3"]
path = C:/Program Files/KDiff3/kdiff3.exe
keepBackup = false
trustExitCode = false
Upvotes: 0