Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save artero/1236170 to your computer and use it in GitHub Desktop.
Save artero/1236170 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

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

Copy link

ghost commented Oct 11, 2016

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

@mladenp
Copy link

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' "

@The-Devhouse-Agency
Copy link

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!

Copy link

ghost commented Jan 4, 2017

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!

@tokra
Copy link

tokra 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
Copy link

trnt2110 commented Feb 2, 2017

Works for me, thanks!

@coder828
Copy link

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

@aminqp
Copy link

aminqp commented Feb 12, 2017

Awesome ! Thank you

@whalemare
Copy link

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

@LaurenQED
Copy link

Thanks tomaspen!

Steps I took to get it to work for Sierra 10.12.5

$open ~/.bash
add to top of bash profile: alias subl='open -a "Sublime Text"'
save bash_profile
$source ~/.bash_profile
$subl .

@tonhwk
Copy link

tonhwk commented Jul 30, 2017

I dont know how but it works! thank you!

@fr6nco
Copy link

fr6nco commented Sep 28, 2017

Verrrry fucking useful!! thank you very much

@quantumproducer
Copy link

@tomespen that won't work with doing subl ~/folder.

@lnshi
Copy link

lnshi commented Dec 31, 2017

Really no need to be this complex, just add one line alias into your .bash_profile, then you are done:

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

subl [YOUR_FILE_PATH]

@whimsycwd
Copy link

Thank you!~

@edensharvit
Copy link

life saver!!!

@gupta-akshat
Copy link

.bash_profile alternate changes

#Custom Path
SUBLIME_PATH=/Applications/Sublime\ Text.app/Contents/SharedSupport/bin

#Append to Path
export PATH=$SUBLIME_PATH:$PATH

#Custom Alias
alias sublime='subl'

@beepony
Copy link

beepony commented Feb 26, 2019

Really no need to be this complex, just add one line alias into your .bash_profile, then you are done:

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

subl [YOUR_FILE_PATH]

thank you, it works

@eguyot
Copy link

eguyot commented Aug 19, 2021

Hello. Thank you for this tutorial. Could someone consider updating it please ?
Since Sublime Text 2 is now named only Sublime Text.

Also, could you add the procedure for zsh ? I can't find something that work properly online.

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