Open a file in Sublime Text via OS X Terminal

If you’re working in Terminal and would like an efficient command to open a file in Sublime Text 2 — as in subl filename.html — here’s how to set it up.

I’ve now edited this post to begin with my favorite method, using an alias, then added a few more notes on creating handy aliases. Last is an alternative method which requires creating a symlink to Sublime Text’s own terminal connection.

Creating an Alias

This, for me, is the easiest method. We’ll create an alias for the basic Terminal command to open a file in Sublime Text 2, which would be: open -a Sublime Text 2 newfile.html

Here’s how to set up an alias so that you only have to type: subl filename.html

From Terminal …

1. Edit the bash profile, by typing:

vim .bash_profile

2. Then type i to insert.

3. Insert this line:

alias subl='open -a Sublime Text 2'

This creates a new shorter version of the longer command. Typing subl will do what’s after the equal sign!

4. Hit the **Escape** key to stop editing.

5. Type :wq to save and quit the editor.

6. Type exit to exit the terminal session.

7. Quit Terminal and restart it.

8. Type subl filename.html to open the desired file in Sublime Text!

To create more aliases, you can now do it by editing the .bash_profile in Sublime Text. Just type: subl .bash_profile

On the Topic of Aliases

Here are a few similar aliases I set up, by adding them to my .bash_profile, to efficiently open files in Firefox, Chrome, Safari, navigate to a frequently used directory, etc.

# Open a file in Sublime Text 2
alias subl='open -a Sublime Text 2'

#Edit Bash Profile in Sublime Text 2
alias bashprof='subl .bash_profile'

# Open a file in desired browser
alias ff='open -a Firefox'
alias chr='open -a Google Chrome'
alias saf='open -a Safari'

# Open file in Firefox, Chrome, and Safari
function testall { ff $1; chr $1; saf $1; }

# Shortcuts to directories
alias home='cd ~/'
alias nodeplay='cd ~/Nodeplay'
alias github='cd ~/Github'
alias sites='cd ~/Sites'

# Change to and list contents of a directory
function cdl { cd $1; ls; }

This Lifehacker post was helpful in the quest:
Become a Command Line Ninja With These Time-Saving Shortcuts – Lifehacker

Another Approach Using a Symlink

Back to the topic of opening a file in Sublime Text 2 — it’s amazing how diverse and confusing are the opinions on this topic. The alias approach (above) seems easiest (and least frequently mentioned). Here is another method that uses a symlink, for those who prefer it.

 

1. Create a directory named bin in your user home directory.

Create bin directory

Now we need to create a symlink in this new directory …

 

2. In Terminal, enter the following line and hit return:

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

This creates a symlink called subl. You’ll see it in Finder inside your newly created bin directory.

Sublime Bin Symlink

Next to add this new directory path to your Terminal bash profile.

 

3. In Terminal, open the bash profile in the vim text editor:

vim .bash_profile

 

4. Then type i to insert:

i

 

5. Type this new export path at the cursor:

export PATH=$PATH:~/bin

Sublime Export Path

 

6. Now hit your keyboard’s Escape key to stop editing.

 

7. Save the file by typing :wq and pressing Return:

:wq

[Press Return]

 

8. Exit the terminal session by typing:

exit

 

9. Quit Terminal, then restart it.

 

10. If you need a quick file to test, you can create it via Terminal:

touch newfile.html

 

11. Now open your new file in Sublime Text by typing:

subl newfile.html

This should be your result!

Sublime File Opened

 

12. To change the command for opening a file in Sublime Text …

Simply change the name of the symlink in your bin directory. For instance, you could change the symlink name to sublime, and then type sublime newfile.html in Terminal to open your desired file.

Sublime Symlink Name Change

 

Spot an update?

Let me know if you spot anything that needs updating or clarification.

 

Credit

Piecing these steps together from available online resources took a bit of work.

Helpful bits can be found in the Sublime Text docs, and in this gist by artero.

Credit for piecing together a working combo goes to @piperseth.

5 thoughts on “Open a file in Sublime Text via OS X Terminal

  1. I have to tell you. I have been working on this rather “simple” task for about four hours. I have tried the sum link, the “bin” file from sf’s website. Nothing has worked. I tried your way – and voila – it works! Thank you for sharing your knowledge, and now I can get on with my project.

  2. Karan — I appreciate your passion for succinctness. Thanks!
    Nick — I’m glad to hear this was helpful!

  3. Thanks for sharing! This makes life in the Terminal so much easier 🙂

    I noticed I had to escape the space in the app name to make this work (on OSX 10.9.3). As in:
    alias subl='open -a Sublime Text'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s