public

Launch Sublime Text 2 from the Mac OS X Terminal

  • Download Gist
launch_sublime_from_terminal.markdown
Markdown

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.

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 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.

I have encountered some erros when execute source .bash_profile, can you help me?

Marvin:~ edmundojr$ source .bash_profile 
-bash: .bash_profile: line 1: syntax error near unexpected token `('
-bash: .bash_profile: line 1: `export PATH=/usr/local:(…)'

Update:
Now I see the “note”, case solved!

Awesome breakdown of this. I also like that I can name the command whatever I like :)

Thanks!

Thx mate,

I was reading the a tips&trick guide of Sublime 2 here: http://net.tutsplus.com/tutorials/tools-and-tips/sublime-text-2-tips-and-tricks/ and I didnt feel very confortable with the bin folder, this solution it´s easier and... why not? more elegant

/clap

I followed these steps but had to create a .bash_profile first. I lost my commands in my terminal when I did this. I am sure it is something that I did creating the .bash_profile but I don't know how to get it back. Any help would be very much appreciated.

Andrew

As Andrew noted, .bash_profile is not on a Mac by default. Just followed these instructions on my Mac. I created a .bash_profile but as it's empty to start the directions here apply to people who already have one it seems. Can you provide an example file?

This is excellent Olivier. Kudos to you! Worked perfectly!

Worked great. Thank you!

Worked great. Thanks!

Just for the record, this line didn't work for me in my new Mac 10.7.4
ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

until I wrapped the location in quotes.
ln -s "/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime

I did as instructed first
ln -s "/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime

Terminal gives the prompt "ln: /usr/local/bin/sublime: No such file or directory"

so I went to check in /usr/local/, turns out there is no directory named "bin" here, but there is one in /usr

thus I changed the line to
sudo ln -s "/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl" /usr/bin/sublime

quit Terminal, then relaunch it
try to use sublime as a command, terminal still tells me "-bash: sublime: command not found"

echo $PATH gives the following result
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

what am I doing wrong here?

Please ignore the last comment.

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

then relaunch Terminal worked for me

or alias:

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

Thanks a bunch i have been looking for somethng like this.

The only "issue" I see with putting the symlink in /usr/local versus ~/bin is that /usr/local is the directory used by Homebrew, and Sublime isn't installed through Homebrew.

This is more philosophical than a real issue, but I guess I like the idea of having Homebrew and everything installed through Homebrew sandboxed to /usr/local. That way if I just remove that directory, I can start from scratch again for everything Homebrew-related, but still have Sublime working from the command-line since I put the symlink in ~/bin.

For more info on Homebrew and /usr/local see "How do I uninstall Homebrew?" and "Why does Homebrew insist I install to /usr/local?" at https://github.com/mxcl/homebrew/wiki/FAQ

I was trying to get this setup on a new machine, and guess what, the sublime documentation wraps the path to subl between quotes, that was causing my symlink to not function as desired.

Saw the unquoted path in this gist, followed it and voila!

Nice! I thought 'sublime' was better than 'subl' too.

Homebrew is usually installed to /usr/local/Cellar. I like the alias approach the best, then you don't have a symlink lying around. Just add this to your ~/.profile (or ~/.bash_profile or ~/.bash_rc)

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

by typing this line:

echo "alias subl='/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl'" >> ~/.profile
source ~/.profile

When I type this: ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime
I get this error: ln: /usr/local/bin/sublime: Permission denied
Help?

@mediter & @paulrossman,

Thanks, the alias code and the sudo code worked for me.

You can also add "export EDITOR='sublime -w'" to your ~/.bash_profile to have ST as your default editor (for commenting git commits, using fc etc)

Everything works great on Mac here but if you create a Bash profile you may find you loose Bash or the commands in your terminal.
I didn't need to do it and if you created an empty one and you are having problems delete and relaunch the terminal app.
I think it is due to a missing or incorrect path directive in the head of the profile file.

Great worth, thanks a bunch.

I looked at so many websites and forums trying to get this to work and this is the only thing that helped. Thank you so much!

Thank you so much for this! I spent a whole day trying to figure it out.

Greatly appreciated. Thank you sir!

Woo just found this! Thanks!

Easy to follow write up, thanks.

Best breakdown of this issue I've found yet, thanks!

I can't find subl, open it, not sure what to look for. In /Applications/Sublime Text.app/Contents/SharedSupport/bin/ I have
drwxr-xr-x 2 ks admin 68 Jan 19 11:22 .
drwxr-xr-x 3 ks admin 102 Dec 16 21:25 ..

$ open subl
The file /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl does not exist.
$ subl --help
-bash: subl: command not found

Could it be as per ST (http://www.sublimetext.com/docs/2/osx_command_line.html) MacPorts are responsible for not allowing it work correctly?

I created a symlink on the ~/bin folder to Sublime Text 2.
Then I added the line "export PATH=~/bin" to .bashrc and runned it.

Now I cannot run any command (not even "ls") except for "sublime [filename]". I get always "-bash: [command name]: command not found"

Why is that? Sorry, I'm a noob on this topic.
thanks

The line you provided replaces the whole PATH chain with only the sublime folder; what you want to do is append it to the folders it already has, which contain all the system utilities:

export PATH=$PATH:~/bin

Removing the quotes around the path worked for me too, as per vishu17's comment above.

The entire post is premised on this working okay: open /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl

But what if the above does not work at all?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.