460

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.

Community
  • 1
  • 1
Gary Ewan Park
  • 13,805
  • 4
  • 29
  • 53
  • Does this answer your question? [How do I make Git use the editor of my choice for commits?](https://stackoverflow.com/questions/2596805/how-do-i-make-git-use-the-editor-of-my-choice-for-commits) – Henke Dec 10 '20 at 08:49

14 Answers14

832

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:

  1. 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.
  2. 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. enter image description here 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 Code
  • git commit allows to use VS Code for the commit message
  • git add -p followed by e for interactive add
  • git difftool <commit>^ <commit> allows to use VS Code as diff editor for changes
leerssej
  • 10,604
  • 5
  • 42
  • 50
rexcfnghk
  • 11,625
  • 1
  • 30
  • 55
  • 94
    I also found addinng `--new-window` to the command to be helpful. This way the git operation will open in a new window. – jrotello Apr 19 '16 at 21:57
  • This is answer is great. Only problem is the code window opens under my current command prompt window. (running code version 1.4.0) – Burt_Harris Aug 09 '16 at 19:23
  • 2
    When we talk about _command line_ we mean `Ctrl+Shift+P` ? Mine is not working (no `code --help` available) and i do not understand what means: "Make sure you selected Add to PATH during the installation" What should i do to check it? Can someone please help me? – Paolo Falomo Aug 31 '16 at 10:52
  • 1
    Related: VS Code still isn't usable as a git mergetool, but hopefully that's coming: https://github.com/Microsoft/vscode/issues/5770 – crimbo Nov 01 '16 at 18:11
  • 2
    Note as of v1.1 you need to disable "hot exit" or remember to explicitly save the file before exiting vscode otherwise git won't see any changes. If you prefer to be reminded to save on exit you can disable this feature by editing your user settings: Insert `"files.hotExit": "off"` at the bottom of your config – Jack Ukleja Mar 02 '17 at 05:00
  • 1
    @jrotello: According to the [Command Line documentation](https://code.visualstudio.com/docs/editor/command-line), `--new-window` is (now) the default, so you no longer have to specify it when running from the command line. – IInspectable Aug 29 '17 at 11:28
  • 6
    I had to use `git config core.editor "code -n --wait"` with VS Code 1.17.2, running from external command line. Both `code` and `code --wait` got me the "Aborting commit due to empty commit message." – Robert Calhoun Oct 28 '17 at 20:25
  • In Linux Mint 19, VSC displays the $LOCAL file, but displays a new empty file for $REMOTE. Both are opened in tabs instead of being displayed side by side. – kas Jul 11 '18 at 02:56
  • Would be great if we could also: 1) Specify that ESC closes the window from the command line (so it's not a permanent key mapping) and 2) From the command line, temporarily disable the confirmation to save when exiting (always save). This would make editing quick: `Commit > Type stuff > ESC > Done` – void.pointer Sep 14 '18 at 15:47
  • For the difftool cmd, you might want to add `--disable-extensions` to disable all extensions or `--disable-extension ` to disable an extension in case extensions keep raising annoying errors in the diff window. You can get the extension-id on the extension detail page like in this image https://code.visualstudio.com/assets/docs/editor/extension-gallery/extension-identifier.png. – ElpieKay Mar 21 '19 at 04:13
  • @PaoloFalomo, _command line_ means from a shell. `ctrl+\`` opens it in vscode, but you could just open cmd or git bash. – Eric Ed Lohmar Aug 05 '19 at 15:00
  • @void.pointer, I think I find it just as fast to use `Commit > Type Stuff > ctrl+s (save) > ctrl+w (close tab) > Done` given that `s` and `w` are right next to each other on the keyboard. – Eric Ed Lohmar Aug 05 '19 at 15:02
  • 4
    And merge is now available as well as: [merge] tool = vscode [mergetool "vscode"] cmd = code --wait $MERGED – Sting Nov 21 '19 at 18:44
  • If you're getting error `line 14: realpath: command not found`, use `code.cmd --wait` instead. – Brad Turek Jan 23 '21 at 06:51
44

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.

Frank Boucher
  • 1,655
  • 18
  • 21
  • Thanks! What is the significance of the `-w` and where did you find that documented? – Gary Ewan Park Oct 28 '15 at 07:22
  • 1
    Good point. It's coming from Atom -w is for Wait. I edited my answer. – Frank Boucher Oct 28 '15 at 14:36
  • I have set this up, and I am happy to say that it is working. If I could get this to work now as Visual Studio Code doing the diffing of the files as well, that would be great! :-) – Gary Ewan Park Oct 28 '15 at 19:19
  • 12
    this now seems to no longer work with the latest update to VS Code. Do you have any ideas? Thanks! – Gary Ewan Park Nov 10 '15 at 20:17
  • 3
    @GaryEwanPark: You need to launch *code.cmd* (or *code* on \*nix) from the *bin* subdirectory instead, for the command line options to work. Ideally you would add the *bin* subdirectory to your %PATH% environment variable (the installer does that for you, too), and configure the editor running `git config --global core.editor "code --wait"`. – IInspectable Aug 29 '17 at 11:19
22

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:

  1. When you working from local terminal.
  2. 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

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.

Ruslan Stelmachenko
  • 3,746
  • 28
  • 43
  • I've discovered that, with Git 2.30.1 on macOS at least, if no `core.editor` config is provided, Git uses the `$VISUAL` environment variable, rather than `$EDITOR`. So I like this answer, but I also added a line below `fi` stating `export VISUAL="$EDITOR"` to sync them up. – Chris Mar 06 '21 at 16:14
15

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.

IluxaKuk
  • 199
  • 1
  • 7
  • 1
    `-n` is short for `--new-window` making git open up a new editor window, and `-w` is short for `--wait`, making git wait for you to close the window again. This is the only answer that works for me, because I if I already have VS Code open `--wait` will not work without `--new-window`. – Jan Aagaard Oct 09 '18 at 06:25
2

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.

miqh
  • 3,326
  • 2
  • 26
  • 34
  • I had also tried that, however, I was getting "fatal: bad config file line 14 in C:\Users\gep13/.gitconfig" then I noticed I had backslashes, where you have forward slashes. Changing those round made it work, but like you I am seeing the "Renderer process started" output, which actually outputs twice for me, and then shows: "[11956:0504/091108:ERROR:ipc_channel_win.cc(136)] pipe error: 109" but the commit actually seems to have worked. – Gary Ewan Park May 04 '15 at 08:12
  • Ah, interesting. It appears that my suggestion only works if you've got Code completely closed _(not very convenient!)_ before making the commit. I encounter the same error you reported in your update if Code is open. – miqh May 04 '15 at 08:15
  • interesting indeed. I guess that is where the -multiInst flag for Notepad++ would come into play. I wonder if there is something similar for VSCode. – Gary Ewan Park May 04 '15 at 08:20
  • Just FYI, `app-0.1.0/resources/app/env.js` appears to contain a few command-line flags that the Code executable takes. None suggest behaviour equivalent to that of `-multiInst` though. – miqh May 04 '15 at 08:51
  • you need to add the `--wait` flag, and the `--new-window` flag is also highly recommended, so that the commit msg/diff/whatever doesn't just show up as a new tab in an already-open editor, requiring you to close files you may still be working on in order to signal git that you're done editing. – Isochronous Jul 21 '17 at 14:41
2

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

Wasif Hossain
  • 3,668
  • 1
  • 15
  • 20
2

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:

enter image description here

(and commit 24f1b69 fixes that)

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
2
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

Gaurav Suhanda
  • 157
  • 1
  • 6
1

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

Şafak Gür
  • 6,389
  • 3
  • 52
  • 87
  • What did you point at as the default editor? I have tried to use what is described here: http://www.donovanbrown.com/post/2015/07/07/how-to-set-visual-studio-code-as-your-default-editor But that doesn't seem to want to work. Don't really want to point at a specific exe, as that will change as Code is updated. – Gary Ewan Park Aug 14 '15 at 06:58
  • 1
    @Gary, I didn't have to point a specific file, Code was in the list when I did `Right click` > `Open with` > `Chose another app`. I would expect it to [register itself](https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx) on setup (and update the registry with every update so shell can find it) but I couldn't find anything other than "Open with Code" context menu item in the registry. So I really don't have a clearer answer for this, sorry. – Şafak Gür Aug 14 '15 at 08:04
  • Hmm, that's weird, I don't have code as an entry in the list that appears when I do that. – Gary Ewan Park Aug 14 '15 at 12:38
1

Run this command in your Mac Terminal app

git config --global core.editor "/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
Mohammed
  • 1,167
  • 3
  • 14
  • 31
  • 2
    This doesn't quite work as you get the error message: Aborting commit due to empty commit message. A wait flag will need to be added. For example --wait. – Belfield Aug 30 '17 at 13:09
1

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"
Victor Ma
  • 11
  • 2
  • I was able to add with: git config --global core.editor "'C:\Users\cbre_admin\AppData\Local\Programs\Microsoft VS Code\Code.exe' -w" – rjt011000 Mar 12 '19 at 15:02
0

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

SJMan
  • 1,468
  • 2
  • 13
  • 36
  • As I mentioned in my question, I don't think merging will be supported, as I don't think Visual Studio Code knows how to do this, I suspect it will only be able to do diff and commit messaging. – Gary Ewan Park May 04 '15 at 07:44
0

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.

Zidu
  • 1
0

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