Sublime Text 3 from Command Line in OSX

Text below is a gist fork of gist by Artero with basic adjustments to use Sublime Text 3 from Command Line in OSX. A fork I have adjusted and updated some more in this blog post.

It explains how you can get Sublime 3 to work from the command line. Something I missed after moving from TextMate to it. Textmate ads it to your path for you, but for Sublime you have to do all the legwork yourself.

Sublime Text 3 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.app/Contents/SharedSupport/bin/subl

You can find more details about subl here: http://www.sublimetext.com/docs/3/osx_command_line.html . Suggestions there are not followed as we do a slightly adjusted setup based on Artero’s recommendations.

Installation

First thing we do is create a symlink to make the app link to the bin folder to trigger it as a command from the command line. This will basically allow the installation of the sublime command. This and one or two more steps to tell Bash where the command is located and how to remember it.

Symlink Creation

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

NB See also SO Thread

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.

Add Path to Bash Profile

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). In my case I had to create a .bash_profile anew using touch.

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

That is what I had to do as I just got a new MBP and therefore had not much history as of yet using command line voodoo.

Source the bash profile if need be

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.

In my case with everything brand spanking new that is just what I had to do.

Testing

Open a Terminal window to test stuff now and run the following command:

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

or run the following command to open a directory:

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

or even the entire current directory:

sublime . (to open the entire current directory)

I love using  this with “.” . Easy to open a whole project like that.

Conclusion

Well, we now have the option to run sublime from the command line and open files or folders. And that is what we wanted. Just done with a basic symlink and addition of the path to the bash profile. Wasn’t to hard really. And it makes our lives so much easier!

 

Tagged in : Tagged in : ,
Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.