I do lots of tutorials and teaching of web development both on this blog and locally to groups in my area. My computer gets a lot of screentime being watched by avid soon-to-be hackers and I answer lots of questions. Surprisingly, one question gets asked more than anything else and it has almost nothing to do with what I am teaching.
Everyone wants to know how to get sublime text to open when you type subl . into the terminal. Today I am going to teach you how to do just that, because it is very simple to set up.
Installing Subl Terminal Command
This really should go without saying, but make sure you have Sublime Text installed. If you are doing it today, then I suggest you install Sublime Text 3 which is actually the beta version (current version is 2.0.2 as of this writing), but I have been using version 3 which has been in beta for well over a year and is 100% stable. You can find the download page for Sublime Text 3 right here.
Now that you have that installed, go into your Finder and find the Sublime Text application in the Applications folder.
Take note the exact name of your Sublime Text installation. It will probably be one of the following:
- Sublime Text 2 if you have version 2
- Sublime Text if you have version 3 (beta)
This may not seem like a big deal but it will be later on. Now that you know, just log that away and we will use it in a second.
Check your $PATH
Next, we need to check our path, just to make sure you don’t have any problems accessing it. Open up your terminal and type the following command:
echo $PATH
This should output something along the lines of /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin if everything is going alright. If you are getting nothing back then there is something wrong with your $PATH file, which may be the reason that these later steps won’t work, since they rely on this. You will want to google how to fix this, as it is outside of the scope of this tutorial.
The critical reason for this step is that you want to make sure that you see /usr/local/bin within that string that was returned. This is a list of locations to look for symbolic links (which is what we are creating). We will be saving our symbolic link in the /usr/local/bin directory, so we want to make sure that our $PATH is set up to be checking for symlinks there. Each location is separated with a colon, so as you can see in the output above (which is the Mac OSX default by the way) we are looking not only in /usr/local/bin but also in /usr/bin and /bin and so on, and all these locations are separated with colons.
Magic Line of Code
Now for the magical line of code to make this work, you will want to type this into your terminal, exactly the way it is below. The only difference will be whether or not your Sublime Text installation is Sublime Text 2 or if it is Sublime Text .
For Sublime Text 3:
sudo ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
For Sublime Text 2:
sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Because we are using sudo (super user authorization) then the terminal will ask you for a password to authorize this and you will need to type your admin password for the computer. Note: there will be no indication that anything is being typed, but it is working, just type carefully and slowly and press enter. If you screw up the password just run the command again (press the up arrow once and then press enter).
Using Subl Command
It should be working. Now it is time to play around. When using the terminal you will want to navigate with your traditional cd and ls commands. When you are inside a folder that you want to open in sublime text, you can simply type:
subl .
This will open the entire folder in Sublime Text that you are currently in. If you want to open just a specific file within the folder then you can follow the subl command with the file name you want to open.
subl index.html
That command would only open the index.html file only, and not the other files in the directory.
The final usage of the subl command is to use in in the same way you would use nano or vim (I swear if someone in the comments tells me that vim is better than Sublime Text, then I’m going to jump off a bridge).
subl ~/.profile
When doing this, you will want to Ctrl + S to save the file and then close Sublime when you are done editing the file. For those Vim users squirming in their pants right now, I will admit that it doesn’t make a ton of sense to use Sublime just to do something like editing a quick config or profile file. The purpose was to demonstrate that you could. For most practical purposes, it would make more sense to use Vim or Nano to edit something in the terminal so you don’t have to leave the terminal and they are sometimes faster to use for quick edits because of their heavy reliance on keyboard shortcuts.
My most common use case of this command is the Subl . command to open a folder in Sublime. Let’s say I am working on a Laravel application, well I would navigate to the folder with the command line, maybe pull with git via the terminal and then I would use Subl . to open the files in Sublime Text to start working.
If you have any problems or if this saved you a ton of time, please comment below to let me know. I appreciate hearing from you, even if its just a “Thanks I got it working”. We cover lots of awesome tips like this on my YouTube Channel, if this was helpful I would suggest Subscribing to the Youtube Channel.
Thank you that!
Quick note that reading text in box is a bit difficult. Thanks for sharing great content.
Thanks for pointing that out. I have fixed it 🙂
It doesn’t work on windows
Windows is different because it is not Unix based like Linux and Mac are.
You’re amazing
Can I install it on Windows?