Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Launch Sublime Text 2 from the Mac OS X Terminal

Launch Sublime Text 2 from the Mac OS X Terminal

Sublime Text 2 ships with a CLI called subl (why not "sublime", go figure). This utility is hidden in the following folder (assuming you installed Sublime in /Applications like normal folk. If this following line opens Sublime Text for you, then bingo, you're ready.

open /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl

You can find more (official) details about subl here: http://www.sublimetext.com/docs/2/osx_command_line.html

Installation

The official documentation I linked to above recommends creating a ~/bin folder (in your home directory). That's weird, I don't recall ever being asked to do that on OS X since most people install binaries within /usr/local/bin which – if you're a developer – is likely to already have tons of other binaries.

So contrary to the Sublime team recommendation, we're not going to create a bin folder in your home directory:

ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

This will simply create a symlink called sublime (remember, we like names that don't suck to type 500 times a day) between the subl binary stashed in the Sublime application package, and a folder where your system usually looks for binaries to execute (launch). Think of it as a wormhole of awesome.

Now let's do a check to see if everything will run smoothly. Enter this:

open ~/.bash_profile

(In some cases the profile file is named ~/.profile)

You should see at the top of the file a line that starts with: export PATH=

This contains all the directories that will be looked into for executable binaries when you type a command in Terminal. Since we create a symlink to subl called sublime in the /usr/local/bin directory let's check if this directory is listed on that same line.

If it is, perfect. Let's keep going. If not, simply add it like this and save the file:

export PATH=/usr/local/bin:(...)

Note: The '(...)' in this example represents other folders that would be listed on the same line and separated by a colon.

If you don't already have a PATH set in your bash_profile you can type:

export PATH=/usr/local/bin:$PATH

If you had to add /usr/local/bin to your PATH, run the following command before continuing:

source ~/.bash_profile

This will reload your .bash_profile with the newly added directory.

Testing

Open a Terminal window and run:

sublime filename (replace "filename" by an actual file name)

or

sublime foldername (replace "foldername" by an actual folder name)

or even

sublime . (to open the entire current directory)

Conclusion

Now you don't need to get out of Terminal to simply open a file or a folder, you didn't have to add an "alias" or yet another bin directory to your .bash_profile which you would have needed with the official instructions given by the Sublime team.

Have fun, Sublime is a great editor showing a lot of promise.

mapkon commented Jul 6, 2012

Awesome awesome editor - not sure how i have survived without it!

Thanks for this, just upgraded to Mountain Lion which must've removed my sublime CLI. I go here every time I need to add sublime to the terminal and open files.

This worked for me, thanks!

For some reason this gives me an error:

$ ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
$ subl --help
-bash: /Applications/Sublime: No such file or directory

Tried escaping the spaces in Sublime Text 2 with \'s, wrapping them in quotes, still get this error. Any ideas?

Worked perfectly--great!

krv commented Aug 23, 2012

For those having troubles with Mountain Lion, create a bin directory first in /usr/local.

$ sudo mkdir /usr/local/bin
$ sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

abachuk commented Aug 24, 2012

Works great for me!!! Thanks

momer commented Aug 28, 2012

Thank you krv

What I really really really like about this gist is you actually explain wtf you are doing it, and why your doing it differently than posted elsewhere ::cough::docs::cough:: Very well done, wish all instructions were this well written / described. Not only did it fix the problem, but also taught me a couple nifty tricks i hadn't thought of. Thank you!!!

I have a new Mac, Mountain Lion and it does not come with a Bash Profile by default. I created a blank one but am confused on what I should add exactly.

Do I add this:

export PATH=/usr/local/bin:(...)

or

export PATH=/usr/local/bin

or

export PATH=/usr/local/bin:sublime

or something else? And do I need anything else in my bash_profile for this to work? Thanks!

Super helpful. Thank you.

If you don't already have a PATH set in your bash_profile you can type:

export PATH=/usr/local/bin:$PATH

edu222 commented Sep 16, 2012

This instructions were very helpful, I was getting frustrated with the original instructions at http://www.sublimetext.com/docs/2/osx_command_line.html

Thanks, I managed to get this working on my Mac, but when not while using SSH. Its giving me:
-bash: subl: command not found

aimango commented Sep 21, 2012

Awesome thanks!

@sguptaet Make sure your bash export path has the sublime path in it. Mine looks like this:

export PATH="/usr/local/bin:/usr/local/bin/sublime:~/bin:$PATH"

+1. Thanks.

SWEEEET!

Thanks!!!

+1. Thanks!

Thanks for this, it helps me work around an issue with running a compass build with rvm from within ST2 WhatWeDo/Sublime-Text-2-Compass-Build-System#6

ButuzGOL commented Dec 2, 2012

Thanks!

U can also add an alias to the bash_profile if u dont dont want to symlink and export path

alias subl="/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl"

and use
example:
subl filename

I have it sublime working out of the terminal, but had to use krvs helpful instructions to get it to work

$ sudo mkdir /usr/local/bin
$ sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

My question out of curiosity is that after using these commands in terminal I then tried the series of tests to see if everything worked smoothly

open ~/.bash_profile .

This command does not work and terminal says that the file does not exist. I would just like to know why this is not working. I am assuming it is somehow related to the Mountain Lion configuration I set it up with?

Thanks

cisliu commented Dec 29, 2012

Awesome! I constantly got "No such file or directory" error when following the default method suggested by Sublime team and couldn't solve it in the past 20 mins. Found this, followed this, boom, everything worked like a charm! Thank you so much!

klipnov commented Jan 8, 2013

It works...Good tutorial

Thanks! This worked for me

tonyv69 commented Jan 17, 2013

Thanks, works well for me, good work me friend!

Thank you for sharing this.

awesome. simple

rowstu commented Feb 7, 2013

awesome

@clickbooq Thanks.

export PATH="/usr/local/bin:/usr/local/bin/sublime:~/bin:$PATH"

Why not just add the following to your .bash_profile?
alias subl='open -a "Sublime Text 2"'

Thanks, Tom! Thats was much easier.

Please excuse me as I am a brand new beginner when it comes to terminal. When trying to manually add "export PATH=/usr/local/bin:(...)" and then sourcing the .bash_profile file I know get "command not found" for anything i try including basic commands. I can also no longer open .bash_profile with any editor, even VIM in terminal. Please advice as no terminal commands work at all.

Thanks!!!

dotRuez, "Please advice as no terminal commands work at all."

You might have solved this already but there might be others like myself who make the same mistake.

Just open the Launchpad and start Sublime Text, navigate to your home directory ( if thats not where you are currently) and you should be able to open the .bash_profile file. then just delete the content of that file. You likely do have to make sure you path to /bin as well as where you wish to create the symlink. Author might want to address this in his guide so that other OSX newbies like my self fall into this "trap".

Thank you!

Rory80Hz commented Mar 5, 2013

Cheers, useful!

jjamison commented Mar 6, 2013

I'm getting a Permission Denied error when I run the first line at the terminal. Any advice?

lopespm commented Mar 6, 2013

Thank you very much, this was very helpful!

jos4uke commented Mar 8, 2013

Thank you , exactly what I needed!

Thanks @artero for posting the gist and @nravichandra for your followup. It helped me get "subl" working, now I can change the command to anything I want.

So I had this working, but just upgraded to Sublime text 3, anyone know how to change the symlink? I tried navigating to usr/local/bin
rm sublime
ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

Very useful. thanks! :)

Rachel thanks so much! fixed my problem!

Thanks. It works well !

And I found that after using subl a terminal window will pop up, and it is some kine annoying since I'm using iTerm2 now.
I tried a more simple way.
alias sublime="open -a /Applications/Sublime\ Text\ 2.app"
It will do the same thing.

kerstin commented Mar 25, 2013

Ha @silvesthu, was looking for exactly that (also on iTerm2 currently) - thanks!

@artero Thanks for the tip. Typing sublime makes more sense to me than subl.

wswld commented Mar 28, 2013

Actually, subl is just shorter. Somehow the workaround described above haven't worked for me since bash profile in Mac OS X is a mess and I'm trying to avoid that, but the simpler method, suggested by @silvesthu worked like a charm. Although I used subl as the alias. My issue was regarding launching Sublime from terminal at all.

@ghost

ghost commented Apr 5, 2013

@artearo please update your instructions taking into account the following:

Some users do not have a /.bash_profile. If they need to create one, they need to add the following, otherwise they will lose the default bash commands:

export PATH="/usr/local/bin:/usr/local/bin/sublime:~/bin:$PATH"

I also found that I had to manually create the sublime directory in /usr/local/bin/sublime

Finally, calling source ~/.bash_profile didn't enable the sublime command. I had to close the Terminal and restart it. If I knew that in the beginning it would have saved me 30 minutes of my time lol!

two thumbs up!

evert commented Apr 18, 2013

I also always put my own custom stuff in a local ~/bin directory. I like the idea that anything I customize to my system happens in ~bin. Furthermore, my ~bin is versioned and shared across systems.

I let OS X and Brew mess with my global stuff, but any quick binaries I may need should remain in my home directory.

Anyone know how to do this for Sublime Text 3?

YZSheng commented Apr 27, 2013

Thanks a lot! Works like a charm! Though in my case, there is no ~/.bash_profile, it's ~/.profile instead. :)

kul1 commented Apr 27, 2013

Thanks it worked

Thanks!

Thanks it worked. But I cant use same terminal to continue working except I kill the process.

barnett commented May 15, 2013

Thank you!

Neat and to the point. Works like a breeze. Thank you!

@blklane, you can add a " &" after the command, so the process runs in "background" and you can continue to use your terminal.

Like this:
sudo sublime /etc/hosts &

worked for me toooo

Tks 4 share, worked for me too

Thanks, alias sublime="open -a /Applications/Sublime\ Text\ 2.app" worked really well. Is there any difference in using this method?

Thanks it worked for me too

RonanOD commented Jun 27, 2013

Very useful. Thanks for the tip!

really appreciate it, thanks krv! I was trying to figure this out and kept getting an error with other directions. this worked perfectly!

This is a great help.

My Mountain Lion Install did not include a /usr/local/bin file, instead it just had a /usr/bin. By using that instead it all worked.
Thanks for the great tutorial :D

pkorac commented Jul 9, 2013

Works like a charm, thx

Thanks for the quick guide. I didn't realize that subl was hidden inside the app bundle... Good work! :)

sgobin commented Jul 10, 2013

Thanks!

Sublime 3

For Sublime Text 3 the subl path is different so the command is:

ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime

Hey, Neither export PATH="/usr/local/bin:/usr/local/bin/sublime:~/bin:$PATH" nor export PATH=/usr/local/bin:$PATH work for me.

So I ended up with put alias sublime="open -a /Applications/Sublime\ Text\ 2.app" into the ~/.profile, and it works great!

YEAH!

Thanks!

thanks

Thanks! great example.

ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

Worked like a charm in Mountain Lion. Thanks so much for the great tutorial!

kentor commented Aug 28, 2013

on mac, recently, opening sublime from terminal has been acting weirdly. i'm using iTerm, and when i open a file using sublime the bar on top still says iTerm even if I have sublime focused. also, typing in sublime isn't typing in sublime, the keys show up in iTerm.

anyone else have this problem?

Well written, much thanks.

I personally like it better to open my files in a new windows, therefore a simple little alias ;)

alias subl="sublime -n"

Works perfectly. Thank you so much!!

soltrain commented Sep 5, 2013

Thank you for the fantastic explanation.

cuixin commented Sep 12, 2013

good solution, tkx so much!

Thanks!

Great, easy and awesome solution. Thanks!

Very helpful.

Was really confused when I kept getting:

FSPathMakeRef(/Applications/Sublime Text 2.app) failed with error -43.

While using:

alias sublime="open -a /Applications/Sublime\ Text\ 2.app"

From a recent download of sublime. I guess new downloads of sublime are not denoted as versioned? So using:

alias sublime="open -a /Applications/Sublime\ Text\.app"

worked fine.

Thanks for the awesome write up!

mnegi commented Nov 16, 2013

Really cool. Worked for me too.

Decent! :)

jhebb commented Dec 2, 2013

Instructions from krv worked for me in osx 10.9 which didn't have a /usr/local/bin folder by default.

Thanks!

Thanks so much for the details instruction.

jwjb commented Dec 28, 2013

@silvesthu, I am running Sublime Text 3 on OS X 10.9.1 Build 13B42 and found your instructions simple and easy and added "alias sublime="open -a /Applications/Sublime\ Text.app" to my /users/your_user_name/.bash_profile with no problem.

works great, thanks!

rewdy commented Feb 13, 2014

Thank you! This was so much more helpful than the official documentation version.

Great guide, but I used the alias trick as the symlink never worked.

Really fantastic instructions. I could never get the official version to work correctly and this only took a few minutes. Thanks for putting this together.

Thank you...great guide appreciate 100%

Is there a way to do this using either Git Bash or Cygwin?

thanks! this is awesome.

ravyg commented Apr 23, 2014

Nice!

watkins commented May 9, 2014

thanks for the help!

Worked like a charm. I was going crazy trying to get "subl" command working. Thank you!

Thank you. It works for me.

thanks

Nifty, indeed. Thanks.

Thank you very much, I found also your instructions to work perfectly.

Surprised that Sublime themselves couldn't provide this for their own tool which I supported by paying to be a registered user.

piplayan commented Jun 2, 2014

Thanks . works perfect for me

Great! Saves me a lot of misery.

kmccoan commented Jun 14, 2014

Awesome.. thanks a bunch

jheg commented Jun 18, 2014

Thanks for this!

emma1994 commented Jul 1, 2014

@clickbooq, Your comment really helped me open sublime in Iterm2.

It feels so good when this article guides you through what you are doing. Many other sources just give you a bunch of commands and those commands lead you to nowhere.

Very helpful, thank you!

Worked great - thanks!

Awesome, that worked for me. Cheers.

pretty nice, i also used
alias sublime="open -a /Applications/Sublime\ Text\ 2.app"
and it worked fine for me.

mludi commented Sep 3, 2014

Thank you so much! Works like a charm

i did so:
create /usr/local/bin
sudo mkdir /usr/local/bin
symlink
sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime
add export PATH
open ~/.bash_profile

export PATH="/usr/local/bin/sublime:$PATH"

Its worked with "sublime" command

changing for "subl"
mv /usr/local/bin/sublime /usr/local/bin/subl

export PATH="/usr/local/bin/subl:$PATH"

@fernandoGuisso thanks for the suggestion of adding the shortcut:
mv /usr/local/bin/sublime /usr/local/bin/subl

I like this way more and it was not working before with just sublime (saved my sanity).

ysyfff commented Oct 14, 2014

awesome!!!

The cli app is called subl for brevity, in my opinion. Look at your command history and see how short most of the command names are in /bin. He's probably saved developers thousands of seconds. 😀

balanza commented Nov 8, 2014

Just a little UPDATE for Sublime Text 3 (Mac OSX Yosemite)
The ST's folder is Sublime Text.app instead of Sublime Text 2.app, thus the command to be run slightly changes into:

ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

Beautiful !

Thanks!

Awesome bro, thanks !

I screwed up. Not sure how to fix it now. I did..

ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/sub1 /usr/local/bin/sublime

sub1 instead of subl

now I have no idea how to fix it. Help would be much appreciated.

man U are awesome, the best and easier way to do it, thanks a lot.

worked perfectly. thank you!

igor972 commented Jan 27, 2015

Works like a charm
<3

Thank you for these instructions.

In case anyone is struggling to run this line posted ON SUBLIME'S DOCUMENTATION (wtf guys)

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

be sure to first remove the quotations around the first arg!

Thanks! I was struggling for ages trying to get this to work then I followed your tutorial and finally, it worked!

Thanks!

Thanks so much!

This was a game changer for me! Awesome!

Thanks!

many thanks, very easy to follow

Awesome, cheers!

argen commented Apr 10, 2015

Thanks for the tutorial!

Awesome. Thanks!

Works perfectly ! :D Thank you !

works perfectly. thanks!

Thanks!
Btw, ST3 seems to have dropped the digit from the app name, so bin is in /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl

Thanks, man!

It worked for me super cool! thank you

Thanks! I'm a happier person now.

cjliu49 commented Jun 28, 2015

I did this and now I keep getting: "-bash: open: command not found". Uhoh. Any help?

andygco commented Jul 19, 2015

I just did the same for Sublime Text 3. It's nearly the same except,

ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

instead for the symlink

Thanks!

Thanks; It works =)

thanks a bunch for this. each time i get a new puter, i forget to do this, then curse when it doesn't work.
you, sir, are one rad motherbleeper :)

Thank you very much!

Deanzie commented Sep 4, 2015

great!

Hello,

Can someone please help me out, I cant get this to work properly. I am able to open sublime from my terminal but cant open specific files. I've tried opening up bash_profile but I've had no luck. picture attached.

Thank you,

screen shot 2015-09-20 at 1 16 47 am

I'm using iTerm2. The command works but it also opens a regular terminal window. Is there any way to prevent this?

nkshah commented Oct 8, 2015

great! was able to use terminal to use subl path and folder / file name

What do we do about El Captain? We can't edit /bin 😢

A life saver, thank you !!

Thank You, just what I was looking for!

Thank you man, it's great !

Thank you so much! This is exactly what I was looking for.

Awesome! Thanks! opening a file or directory using sublime text from terminal has now become super easy :D I will forget VI forever now

ggrrll commented Jan 19, 2016

thanks to @tomespen

for some reason ARTERO's suggestion was not woking for me...

tutley commented Jan 19, 2016

This is great!

It's worked perfectly! Thanks a lot!

Thank you so much 👍

Thanks, save to 🔖

Awesome!

For ST3 use
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl

Note that I removed the backslash from above and now the symlink will work.

Thanks for putting this together! Finally able to start working on a project in a far more convenient manner.

awesome.... Thanks.....

szberko commented Feb 25, 2016

awesome and simple! Thank you!

erav commented Mar 20, 2016

the official documentation lists a very useful little option which creates a file from whatever you type into stdin:
subl -
this can be used for instance to write to a file the output of any command you type at the terminal (for instance ls):
ls | subl -

For some reason the version of subl included in the "Sublime Text 2" application bundle isn't working for me on OS X 10.11.2 El Capitan. So I ended up writing a shell script to replace it which actually works:

#!/bin/bash

for FN in "$@"; do
  if [ "$FN" = "-" ]; then
    TF=$(mktemp -t "subl stdin").txt
    cat - >"$TF"
    "$0" "$TF"
  else
    if [[ ! "$FN" =~ ^/ ]]; then
      FN="$PWD/$FN"
    fi
    FN=${FN/\"/\\\"}
    FN=${FN%/.}
    eval "osascript -e 'tell application \"Sublime Text 2\"' -e 'activate' -e 'open POSIX file \"$FN\"' -e 'end tell' -e 'return'"
  fi
done

While making an alias to open -a /path/to/Sublime Text 2 works for many cases, it doesn't work for subl -.

neo108 commented Apr 7, 2016

brilliant! Thank you!

aks0 commented Jun 10, 2016

thanks!

maelsan commented Jun 27, 2016

You saved my day. 👍

Worked like a charm ... thanks

danyim commented Jul 17, 2016

For Sublime Text 3 users, this worked for me
ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

GNJM commented Jul 24, 2016

Thanks!

ccostel commented Aug 17, 2016

Thanks! Really awesome stuff.

Thanks for sharing this. However, I like to play with aliases

~$ grep -i sublime ~/.bash_profile
alias sl='/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl'
~$

3pns commented Sep 4, 2016

Awesome ! Thank you

Hi ! I'm getting "permission denied" from the first step. How can I get rid of this ? Thanks a lot :)

@NathaLalou simply enter "sudo !!" after you get "permission denied"

sudo !! is not a fix for the permission denied problem. does anyone have a legitimate solution for getting a permission denied message? i am an admin user.

Woot! thanks for this

buluxan commented Oct 6, 2016

working for me thx @vikas027

~$ grep -i sublime ~/.bash_profile
alias sl='/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl'
~$

Getting the permission denied after the first step! Please help!

mladenp commented Nov 1, 2016

This is crazy complex and not working.
So i just added alias in .bash_profile like so:
alias = "open -a 'sublime text' "

In case anyone is having trouble this instruction is for Sublime Text 2. If your app doesnt have a number at the end of the file name change the following:

ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

To:

ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

in hindsight it seems obvious but caught me up for a bit!

pooley999 commented Jan 4, 2017 edited

MacOS sierra Method:

open /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl 

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

echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile

source ~/.bash_profile

done!

to-kra commented Jan 12, 2017

On macOs you can simply define alias in .bash_profile e.g.: alias sublime='open -a "Sublime Text"'
everytime you open terminal, .bash_profile is sourced, and you simply can use sublime command. No need to make symlinks or export it to path. This solution is simples I ever saw. Thx

trnt2110 commented Feb 2, 2017

Works for me, thanks!

@krv Exactly what I needed, 5 years later. Thanks!

aminqp commented Feb 12, 2017

Awesome ! Thank you

Russian translate below
Перевод на русский, кому надо, быстрее и проще
https://gist.github.com/whalemare/eb28ea4e4f45afeda33a9258c088504a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment