John Abraham
John Abraham

Reputation: 18811

How do I set up a link to open up Visual Studio Code from terminal on OSX?

I've recently installed Visual Studio Code and I love it! In the past, I've used sublime text 2/3 and at some point I copied code to allow subl . command to open the current directory with sublime.

Question:

Desired alias/link/command would be code .

Does the command on this video

Upvotes: 6

Views: 8238

Answers (4)

suryakrupa
suryakrupa

Reputation: 4122

Visual Code has a self-service means to do the same!

Followed https://code.visualstudio.com/docs/setup/mac#_installation

Enable <code>code</code> to open visual-code on mac

Upvotes: 9

Benjamin Pasero
Benjamin Pasero

Reputation: 124174

From version 1.0 use the command Install 'Code' command in path from the command palette (View | Command Palette) to make Code available to the command line.

Historical answer:

With VSCode 0.3.0 the startup script should now be configured to be this:

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

UPDATE: If this doesnt work for you uninstall VSC and reinstall it; for this will only work with 0.3.0+

Upvotes: 10

baf
baf

Reputation: 4681

Update

As pointed out in comments by Tony, Atom Shell has been renamed to Electron. My updated code should read:

code() { (/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron "$1" &) }

Also, I recommend using official way, as described in Benjamin's answer:

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

You should create a command to start the editor in ~/.bash_profile file. This file is read each time you open terminal and start your bash session.

As the process should be started in the background and we want to pass a directory or a file as an argument I would add such single line function to .bash profile:

code() { (/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Atom "$1" &) }

Where /Applications/Visual\ Studio\ Code.app is a path to your Visual Studio Code app. You may need to adjust it if you installed it somewhere else.

This function opens Atom editor that is a base of Visual Studio Code and passes the first parameter to it with $1 expansion. Ampersand & will make the process detach from terminal and run in the background. The whole function body is put in brackets () to quiet messages about detaching and ending the process.

If the .bash_profile file is missing on your system you will have to create it first.

After editing the file you will have to restart your current bash session.

Upvotes: 4

Michael Welch
Michael Welch

Reputation: 1784

I set it up following the docs here: https://code.visualstudio.com/Docs/setup and added a code definition to my .bash_profile.

UPDATE (6/10/2015): This answer originally contained the code from the linked site. I have now removed the code from this answer as the linked site now recommends a different code implementation. (as Benjamin pointed out in the comments). Please see the link for the recommended code to add to your .bash_profile.

Upvotes: 3

Related Questions