Skip to content

Instantly share code, notes, and snippets.

@olivierlacan
Created September 5, 2011 15:50
Show Gist options
  • Save olivierlacan/1195304 to your computer and use it in GitHub Desktop.
Save olivierlacan/1195304 to your computer and use it in GitHub Desktop.
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.

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.

@kmjungersen
Copy link

👍

@luka22
Copy link

luka22 commented Oct 7, 2014

thank you, really helpful, especially 'sublime foldername'.

@ltfschoen
Copy link

thanks, this was a great help!

@tonygaetani
Copy link

Can someone explain to me why I can't use stdin for filename?

for example

sublime -n file.txt

works fine, but

echo file.txt | sublime -n

opens a new file with stdin as the contents. What am I doing wrong?

@gusk
Copy link

gusk commented Nov 13, 2014

As a result of following the instructions step-by-step, I was unable to use typical terminal commands. I am running Yosemite.

Here's what I did differently:

as @lemonzi mentioned, include $PATH:~/ and add this to your ~/.bash_profile

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

In addition, if you're using Sublime Text 3, this is the correct command for the symlink:

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

Note: I am setting the symlink alias to subl, not sublime, as we are referring to the Unix Executable File: .../Contents/SharedSupport/bin/subl

Although this was a great work around to avoid creating a ~/bin directory, the problems outmatch the solution. After spending the necessary time, I probably would've followed the Sublime Developers suggestion.

@tonygaetani
Copy link

@gusk are you sure the application name is "Sublime Text" (/Applications/Sublime\ Text.app) and not "Sublime Text 3" or something like this?

@sineld
Copy link

sineld commented Dec 2, 2014

worked for me. thanks.

@Ducain
Copy link

Ducain commented Mar 6, 2015

Perfect - thank you.

@EuniceP
Copy link

EuniceP commented Mar 9, 2015

Wonderful! As suggested, adding alias and export commands in .bash_profile works like a charm! Thanks!

@EricMentele
Copy link

This solution worked great. I had to step through it carefully having just done a wipe/fresh install of Yosemite. No usr/local/bin or .bash_profile when you have a fresh setup. Gratitude to you good sir! ;)

@famartel
Copy link

It worked perfectly. Stay awesome!

@fabritw
Copy link

fabritw commented Apr 4, 2015

The bash_profile edit caused me to loose all bash commands!
i.e. export PATH="/usr/local/bin:(...)"

Had to fix it using this...
http://stackoverflow.com/questions/12396161/how-to-fix-bash-profile-when-terminal-wont-work

"Pressing Cmd-Shift-. in the file open dialog of TextEdit (or a real editor) should display dot-files in the dialog."

Copy link

ghost commented Apr 13, 2015

Thankyou @gusk

@RedSoxFan22
Copy link

Perfection! Thank you!

@mmikhan
Copy link

mmikhan commented May 8, 2015

A perfect explanation of everything. I didn't want to create a bin directory at my home directory as all the other binaries place it /usr/local/bin/. 😄

@kevglynn
Copy link

kevglynn commented Aug 9, 2015

Great lesson.

@walter211
Copy link

where can i find all command for subl such like --command with what kinde of args

@annrayc
Copy link

annrayc commented Aug 16, 2015

thanks worked great!

@hankcouture
Copy link

Thank you! After much searching online, this one worked great for me.

@saposki
Copy link

saposki commented Oct 10, 2015

This was very straight forward
Thank you.

@Atlas7
Copy link

Atlas7 commented Nov 4, 2015

Awesome. Worked for me. I like this very much :)

@Faline10
Copy link

Faline10 commented Jan 7, 2016

sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl worked for me on my new El Capitan OSX. Note: this allows me to use "subl" rather than "sublime" as a command.

@arzmir
Copy link

arzmir commented Apr 12, 2016

As some have mentioned you might lose access to the normal terminal commands when doing it like this.
This is because you overwrite the standard PATH-variable with the one you have in your .bash_profile when you source it.

The way to avoid it is to extend the PATH-variable instead.

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

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

This way you append '/usr/local/bin' to the already existing path. :)

Copy link

ghost commented May 9, 2016

OSX EICaption with Sublime Text 3

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

➜ ~ vi ~/.bash_profile

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

➜ ~ source ~/.bash_profile
➜ ~ subl

it works fine!

@adamwlev
Copy link

Your the best 💯

@iMagesh
Copy link

iMagesh commented Jun 28, 2016

Cool. Thanks.

@pilgrim2go
Copy link

Cools. Thanks

@desmond132518
Copy link

@jnuc093 It works in MacOSX Sierra too, thanks!

@siwka
Copy link

siwka commented Mar 21, 2018

In new macOS High Sierra it did not work. I had to use "sudo" command that I do not like to use on Mac.
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

@kasem777
Copy link

Awsome question also
For Sublime Text 3 :
sudo ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime

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