running vs code from the command line on mac

Today was my FIRST in person FRC (FIRST Robotics Challenge) meeting since COVID showed up. Very exciting! Right now the older students are teaching the younger students. Today was an excellent intro the the UNIX/Git Bash command line. Towards the end, they showed that you can type “code” at the Git Bash prompt and have VS Code open automatically. Then a student asked how to do it on Mac.

There’s an easy way to do it and a hard way to do it. I went with the hard way because I thought I knew what to do. If we had gone the easy way, we would have been done in the meeting. Which proves the point of the importance of reading documentation.

The easy way

Courtesy of the setup docs for Mac

  1. Open VS Code (using Spotlight Search/the UI)
  2. Open the command palette (command + shift + p)
  3. Type path
  4. Choose option Shell Command: Install ‘code’ command in PATH┬ácommand.
  5. Close VS Code
  6. Open a new terminal window
  7. Type code

The hard way

I divided the hard way into five steps to explain what to do. We got through step 1 and the beginning of step 2 during the meeting.

The Hard Way – Step 1: Find VS Code

I expected VS Code to be in /Applications. On my computer, it is at /Applications/Visual Studio Code.app. That was not where the student had it. After some googling and failed attempts at using Stack Overflow threads, we found an excellent tip.

  1. Hold option and click the Apple in the top nav
  2. Choose “System Information”
  3. Software > Applications
  4. Wait patiently. No really. Wait some more
  5. Scroll all the way down to “V” and click “Visual Studio Code”
  6. Read the location. It was under <user home>/wpiilib/vscode or something like that. (WPILive is the robotics library)

The student had the good idea to open it in Finder and drag to /Applications. (Open finder and hold option while choosing “Go”) for extra options.

The Hard Way – Step 2: Figure out where “code” executable is and add to PATH

We poked around in the Visual Studio Code.app folder but ran out of meeting time. At home, I tried again; this time using the find command

% pwd
/Applications/Visual Studio Code.app/Contents
% find . -name code -print
./Resources/app/bin/code
./Resources/app/out/vs/code

Ok. Then. It’s under Contents/Resources/app./bin/code in side the app directory. I knew it was somewhere in there.

The Hard Way – Step 3: Add to PATH

If you are still on the old shell, add the following line to your <user home>/.bash_profile file. If you’ve switched to zsh, use the .zshrc file instead.

export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"

The Hard Way – Step 4: Reload the file

Run one of these to run the file you just edited and have your change take effect

source .bash_profile
source .zshrc

The Hard Way – Step 5: Launch VS Code

Run!

code

Leave a Reply

Your email address will not be published.