Sublime Text shortcut on command line (subl)

TLDR: Create a ~/bin directory and symlink the Sublime command:

mkdir ~/bin

ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ~/bin/subl

Note: if ~/bin is not in your $PATH, add it to your zshrc or bash_profile:

echo '# add ~/bin to path for scripts like subl\nPATH=$PATH:~/bin' >> ~/.zshrc && source ~/.zshrc

The Sublime Text documentation “OS X Command Line” references the command line tool, subl to work with files on the command line.

This allows you to open the entire current directory in sublime like this:

$ subl .

Screen Shot 2020-04-05 at 9.38.38 AM

This allows you to open a specific file or create a new file like this:

$ subl <file_name>

But subl is not available by default

The command $ subl is not immediately accessible when you download Sublime. Without symlink-ing the command, you will have to type the full path ​to the original subl

This is how you would use the full path to open a file.

$ /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl <file_name>

Where does your computer look for commands? 

Your computer will look for commands in your “load paths”. Find your current load paths by running this command:

$ echo $PATH

You will see all load paths including these default OS X paths:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

You can list the commands in any directory: $ ls /usr/bin

Step 1: Create a symlink in your “~/bin” directory

We will place this symlink in the ~/bin directory. This is simpler because it does not require admin or root access.

Run this command in your terminal:

ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ~/bin/subl

What happened?

ln means link, -s means we want a symbolic link, which will act as a shortcut to the original command within the Sublime Text application.

We will create a symlink at ~/bin/subl that will point to the original Sublime command. Deleting this link will have no effect on the original file.

Step 2: Troubleshooting

Not found: If ~/bin is not in your $PATH variable your will get an error.

Note: ~/bin is different than /bin

Append ~/bin to your $PATH anywhere in your bash profile or zshrc

PATH=$PATH:~/bin

Screen Shot 2020-04-05 at 9.36.52 AM

Still not found: You have to create a new instance of your terminal to see the changes. Alternatively your can run source ~/.bash_profile or source ~/.zshrc to re-run your shells startup configuration