Sublime Text 2: Opening files and folders from the command line

This is a quick tip which I picked up at work recently. But first, a quick disclaimer. I used to be terrified of the command line and was always in awe when seeing other seemingly doing everything in it. The truth is it really isn’t all that bad.

This post is all about how to open files and folders in Sublime Text 2 with a simple command.

Getting set up

Right, with that out of the way. The first thing you need to do is ensure that you have ~/bin added to your path. Don’t runaway just yet, I’ll show you how its done.

Just open terminal and paste in the following line:

1 $ echo "export PATH=~/bin:$PATH" >> ~/.profile

Next up. Adding the subl command. Again, just paste this line into terminal:

1 $ ln -s "/Applications/Sublime Text" ~/bin/subl

Now, to check that has worked properly. Type the following into terminal:

1 $ subl

Sublime should now open. If so this has worked. If not, go back to the beginning and try again.

Why do this?

Well, if you’re a professional developer the chances are you are going to come face to face with the command line more and more so the sooner you are comfortable with it the better.

The benefits of having this command are that you can open Sublime, files and folders, all with a simple command. For example:

This command opens sublime:

1 $ subl

This command opens a folder called mySite:

1 $ subl ~/Documents/mySite

And finally, this command opens a file called index.html:

1 $ subl ~/Documents/mySite/index.html

Obviously you will need to make sure that the path you have specified leads to the directory or file you intend to open.

Where next?

This isn’t the limit of this command but this is only a quick tip that I wanted to share so I am not going to go into everything, but if you are interested simply type:

1 $ subl --help

And you will get a list of all the uses of this command. Have fun!

Leave a comment

About me

I'm a front-end developer based in London. I currently work at MetaBroadcast building JavaScript applications powered by metadata, both on the front-end and with Node.

More about me…