Skip to content

Instantly share code, notes, and snippets.

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

Using Sublime Text 2

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

Using Sublime Text 3

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

You can find more (official) details about subl here.

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.

Create a symbolic link

So contrary to the Sublime team recommendation, we're not going to create a bin folder in your home directory. Open up Terminal and enter the following:

Using Sublime Text 2

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

Using Sublime Text 3

ln -s /Applications/Sublime\ Text.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.

Verify if the Terminal checks the path

Now let's do a check to see if the Terminal really does check the /usr/local/bin/ folder for binaries to execute. If it does, then everything will run smoothly. To do this, we open up the Terminal and type the following:

echo $PATH

and it should show some text that looks like the following:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

If it contains the following directory /usr/local/bin then all is well; the Terminal will check that folder for binaries. Skip the next part and go straight to testing.

Add the missing path

In case the directory is not part of it, we need to add it to your bash profile. In your Terminal, 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 you can't find this line in the file, 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.

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