Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Visual Studio Code as my Mac's default editor (including editing Git commits)

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

like image 504
dumbledad Avatar asked Oct 24 '15 07:10

dumbledad


1 Answers

Add export EDITOR="code -w" to your bash profile

(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. Then do the first step.

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.

like image 199
Govind Rai Avatar answered Oct 13 '22 00:10

Govind Rai