vs code: adding code to your path

2020-08-04

 | 

~4 min read

 | 

639 words

Whenever possible, I avoid using my mouse. This means I’m constantly on the lookout for efficiencies I can gain from operating within the command line (and why vim holds endless fascination for me, even if I haven’t yet made the time to master it). One of my favorite little efficiencies is opening up my text editor of choice (VS Code) from the command line:

$ code . # opens the current directory in VS Code

This only works, however, if you’ve previously configured code to be part of Path.

The easiest way to do this is within VS Code itself by accessing the command palette (⌘ + ⇧ + P) and searching for “Shell Command”:

VS Code Command Palette

Today, however, I was not satisfied with this working. I wanted to know how.

As I’d discovered when learning Rust, the [PATH is really powerful and easily modifiable]((path-variable-modifying). Even better, it’s not nearly as daunting as I originally thought.

After running the installation and getting the “success” message from VS Code, I looked at my PATH variable:

$ echo $PATH

Alas, inspecting the printout to the console, nothing stood out as having changed!

/Users/stephen/.pyenv/shims:/Users/stephen/.nvm/versions/node/v12.18.2/bin:/Users/stephen/.poetry/bin:/Users/stephen/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Users/stephen/.pyenv/shims:/Users/stephen/.nvm/versions/node/v12.18.2/bin:/Users/stephen/.poetry/bin:/Users/stephen/.cargo/bin:/Users/stephen/go/bin:/Users/stephen/.cargo/bin:/Users/stephen/go/bin:/Users/stephen/.poetry/bin

It turns out that VS Code doesn’t get added directly to the PATH. Instead, it gets added as a line to the /usr/local/bin (knowing which path it got added to was a bit of guesswork). To see it, we can go to that location.

$ cd /usr/local/bin
$ ls -la

Then, when we print out the directory’s contents, we see code (and all of the other programs I have added to the PATH):

total 0
drwxrwxr-x  227 stephen  admin  7264 Jul 12 13:14 .
drwxr-xr-x   17 root     wheel   544 Jun 10 18:45 ..
-rw-r--r--    1 stephen  admin     0 Mar 12 07:27 .keepme
lrwxr-xr-x    1 stephen  admin    31 Apr 27 14:48 2to3 -> ../Cellar/python/3.7.7/bin/2to3
lrwxr-xr-x    1 stephen  admin    35 Apr 27 14:48 2to3-3.7 -> ../Cellar/python/3.7.7/bin/2to3-3.7
lrwxr-xr-x    1 stephen  admin    44 Mar 15 14:43 acorn2sfd -> ../Cellar/fontforge/20190801_1/bin/acorn2sfd
lrwxr-xr-x    1 stephen  admin    36 Apr 27 14:42 autoconf -> ../Cellar/autoconf/2.69/bin/autoconf
lrwxr-xr-x    1 stephen  admin    38 Apr 27 14:42 autoheader -> ../Cellar/autoconf/2.69/bin/autoheader
lrwxr-xr-x    1 stephen  admin    36 Apr 27 14:42 autom4te -> ../Cellar/autoconf/2.69/bin/autom4te
lrwxr-xr-x    1 stephen  admin    38 Apr 27 14:42 autoreconf -> ../Cellar/autoconf/2.69/bin/autoreconf
lrwxr-xr-x    1 stephen  admin    36 Apr 27 14:42 autoscan -> ../Cellar/autoconf/2.69/bin/autoscan
lrwxr-xr-x    1 stephen  admin    38 Apr 27 14:42 autoupdate -> ../Cellar/autoconf/2.69/bin/autoupdate
lrwxr-xr-x    1 root     admin    22 Apr 28 11:48 aws -> /usr/local/aws-cli/aws
lrwxr-xr-x    1 root     admin    32 Apr 28 11:48 aws_completer -> /usr/local/aws-cli/aws_completer
lrwxr-xr-x    1 stephen  admin    28 Mar 11 15:41 brew -> /usr/local/Homebrew/bin/brew
lrwxr-xr-x    1 stephen  admin    41 Apr 18 13:07 cairo-sphinx -> ../Cellar/cairo/1.16.0_3/bin/cairo-sphinx
lrwxr-xr-x    1 stephen  admin    40 Apr 18 13:07 cairo-trace -> ../Cellar/cairo/1.16.0_3/bin/cairo-trace
lrwxr-xr-x    1 stephen  admin    27 Mar 15 14:42 cjpeg -> ../Cellar/jpeg/9d/bin/cjpeg
lrwxr-xr-x    1 stephen  admin    39 Mar 12 07:28 clusterdb -> ../Cellar/postgresql/12.2/bin/clusterdb
lrwxr-xr-x    1 stephen  admin    68 Jul 11 12:40 code -> /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code
...

What VS Code did then is actually create a symlink to its binary which is in the /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code.1

And with that, we can now understand how VS Code adds itself to PATH without being explicitly listed. And, based on the length of the list - it’s a good thing too! The number of programs can grow quite large!

Footnotes

  • 1 This is actually the first time I’d ever noticed that the first position of the long format for ls could hold anything other than - or -d. From the manual page on ls, there are actually several options:

    The entry type character describes the type of file, as follows:

       b     Block special file.
       c     Character special file.
       d     Directory.
       l     Symbolic link.
       s     Socket link.
       p     FIFO.
       -     Regular file.

Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!