Accessing Sublime Text from Terminal

After a few years of poking around with text editors, I think I’ve finally settled on Sublime Text (2). It looks good out of the box and is very easily extensible. I’ll get into more detail on that in a future post – for now all I want to show how to access sublime text from BASH or Terminal on a Mac simply by setting up a symlink. Open terminal on a Mac (or your shell of choice – I use iTerm2) and copy/paste (or type carefully!):

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

You can find this documented on the sublime text docs.

The ln command on *nix shells (and others as well) creates a link. From the manual:

The ln utility creates a new directory entry (linked file) which has the
same modes as the original file.  It is useful for maintaining multiple
copies of a file in many places at once without using up storage for the
“copies”; instead, a link “points” to the original copy.

The “-s” option creates a symlink – effectively giving access to a file whilst in directory different to where the file actually lives. It’s basically a portal from one folder to another. All that’s happening in the above code is that the commandline version of the application (subl) is being symlinked into the environment path so that the shell has access to it.

Once symlinked, all you have to do is call subl and pass in your arguments. The default is a filename. Here, I’m opening my bash profile (to add an alias) using subl:

subl ~/.bash_profile

Version 3 of Sublime Text is in beta – will be posting about that soon as well!

Leave a Reply