48

I am trying to configure my Mac Book Pro (OSX El Capitan 10.11.1) to use Visual Studio Code as its default editor. I have created a ~/.bash_profile file with the following two lines

vscode () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
export VISUAL=open\ -n\ -b\ "com.microsoft.VSCode"

This works for some things: I can type vscode test.txt at the bash terminal and up pops test.txt in Visual Studio Code, and if I run the command env I see VISUAL=open -n -b com.microsoft.VSCode in the list. I can even just type $VISUAL and Visual Studio Code opens on a new empty file.

But if I type git commit I get the following error

error: cannot run com.microsoft.vscode: No such file or directory
error: unable to start editor 'com.microsoft.vscode' Please supply the
message using either -m or -F option.

So I have succeeded inasmuch as git is trying to open Visual Studio Code for me to edit my commit message but it is then failing.

What X should I use in the line export VISUAL=X in my ~/.bash_profile file to enable git to open Visual Studio Code for commit messages?

(N.B. How to use Visual Studio Code as Default Editor for Git is not a duplicate since Gary is on a Windows PC.)

Community
  • 1
  • 1
dumbledad
  • 16,305
  • 23
  • 120
  • 273

6 Answers6

66

Add export EDITOR="code -w" to your shell's profile or rc file

(For example, your bash profile is accessible via open ~/.bash_profile)

This requires you to have the code binary already in your path.

If you don't have that, or don't know if you do, simply go into vscode, enter CMD + SHIFT + P, type code and click Shell Command: Install 'code' command in PATH.

Also, very helpful comment by pompalini below,

remember to "refresh" terminal by closing and opening it again or resourcing your bash profile by running source ~/.bash_profile. Only then will the new changes in .bash_profile apply to your terminal.

Govind Rai
  • 14,406
  • 9
  • 72
  • 83
  • 2
    This works, but just remember to "refresh" terminal by closing and opening it again, only then it will apply your bash_profile. Thanks – pompalini Dec 17 '18 at 22:22
  • 7
    You can also run `$ source ~/.bash_profile` instead of opening and closing the terminal. – Bryan Dimas Jul 17 '19 at 17:23
25
  1. In terminal

    • Type: open ~/.bash_profile

    • Insert: export EDITOR="code -w"

  2. In visual studio code

    • Press: CMD + SHIFT + P to open the Command Palette
    • Type install code and select from autocomplete menu shell command: Install 'code' in command PATH
Raine Revere
  • 30,985
  • 5
  • 40
  • 52
Denver
  • 360
  • 3
  • 4
11

It's working with the latest version 0.10.9 of VS Code

[core]
editor = '/Applications/Visual Studio Code.app/Contents/MacOS/Electron' -w

Test it with: git config --global --edit. remember to refresh the terminal after you have changed the config file.

Martin Andersen
  • 2,460
  • 2
  • 38
  • 66
  • Thanks Martin, do you know what the equivalent line would be on Windows? – dumbledad Mar 04 '16 at 15:40
  • It's close to the same [core] editor = 'C:\\Program Files (x86)\\Microsoft VS Code\\code.exe' -w [ – Martin Andersen Mar 04 '16 at 19:42
  • Strangely that didn't work for me on the PC: VS Code started fine but Git did not notice the saved commit message and aborted the commit. I'll ask a separate question – dumbledad Mar 05 '16 at 06:50
6

1) Just add this to your ~/.bash_profile or ~/.zshrc:

code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

2) Then either open a new terminal or run: source ~/.bash_profile or source ~/.zshrc

After this steps, you will be able to do code . to open VS Code on any path

Americo Savinon
  • 2,569
  • 1
  • 19
  • 16
4

You could also use duti, a command line tool to select default applications for document types and URL schemes on macOS.

  1. Install duti via Homebrew:
brew install duti
  1. Use duti:
# Make VS Code as default editor for all text files
duti -s com.microsoft.VSCode public.plain-text all

# Open files without extensions with VS Code as well
duti -s com.microsoft.VSCode public.data all

Reference

TomBen
  • 302
  • 3
  • 6
3

Currently VSCode can not be used as git editor, sorry. We have this as a story on our backlog.

Update for our VS Code 1.0 release:

This is now possible! All you need to do is to configure Code as the git editor using the newly introduced --wait option from the command line.

Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54