Open Sublime Text from Terminal in macOS
Solution 1
I finally got this to work on my OSX box. I used these steps to get it to work:
-
Test
subl
from your ST installation:First, navigate to a small folder in Terminal that you want ST to open and enter the following command:
/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl .
NOTE: You may need to replace
Sublime\ Text.app
in the command above toSublime\ Text\ 3.app
orSublime\ Text\ 2.app
depending upon where the application is stored in yourApplications
directory. The.
at the end of the above command opens the current working directory you are located in (again make sure you're in a directory that only contains a few files!).If you DO NOT get Sublime Text opening your current working directory then the next set of steps will NOT work. If nothing happens or you get an error from Terminal it will be because it couldn't find the Sublime Text application. This would mean that you would have to check what you've typed (spelling, etc.) OR that Sublime Text isn't installed!
-
Check ".bash_profile":
Now it's time to create your symbolic link in your
PATH
folder, BUT, before we do, let's check your profile file by usingnano ~/.bash_profile
. These are the following lines that pertain to havingsubl
work on the command line for Sublime Text:export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH export EDITOR='subl -w'
The first line sets the location where you want Terminal to look for binaries on your machine, I'm going to store my symbolic link in the
/usr/local/bin
directory - I guess you could store it anywhere provided you've notified Terminal where to look for binaries.The second line is OPTIONAL and just sets Sublime Text as the default editor. The flag
-w
has been added and you can find out more about flags by going to the Sublime Text docs: ST4 subl, ST3 subl or ST2 sublIf you do make any edits to this file once you have closed it, you need to run the command:
source ~/.bash_profile
to compile your newly applied edits. If you see any errors after sourcing your file get them fixed before moving to the final step.
-
Create a symbolic link to Sublime Text:
Now in your chosen path (I used
/usr/local/bin
) you now enter the following command:ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
The
/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl
being EXACTLY the same location as what you entered and verified as working in STEP 1 above. The/usr/local/bin/subl
being the location of where you want the symbolic link to be located - needs to be one of yourPATH
locations from STEP 2 above.Now when you navigate to a folder or file that you want to open in Sublime Text you now just enter
subl
followed by the name of the file or.
to open the current working directory.
Solution 2
For MAC 10.8+:
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
worked.
Solution 3
Assuming:
- You have already installed Homebrew.
- /usr/local/bin is in your $PATH.
- You are on Yosemite or El Capitain.
MacOS Sierra 10.12.5 works as well confirmed by David Rawson and MacOS Sierra 10.12.6 confirmed by Alexander K.
Run the following script in Terminal to create the specific symlink.
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Then:
subl .
Hit Return and it should instantly open Sublime Text.
Solution 4
I'm using oh-my-zsh on Mac OSX Mavericks and the symbol link didn't work for me, so I added an alias in my .zshrc file instead:
alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"
Open a new terminal and you should be good to go, and type subl
.
Solution 5
This worked for me (I'm using OS X Mavericks)
First, create a symbolic link:
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/bin/subl
Now you can open sublime with
subl "/a/path/to/the/directory/you/want/to/open"
user1405049
Updated on July 08, 2022Comments
-
user1405049 almost 2 years
In Terminal when I use
.subl
It returns
-bash: .subl: command not found
Anyone know how to open Sublime Text 3 from the command line in macOS?
-
user1405049 about 11 yearsIt returns
ln: /Users/###/bin/subl: No such file or directory
-
danmanstx about 11 yearsdid you look at the link at the bottom of my answer? try to following command
ln -s "/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl" ~/bin/subl
-
rs77 about 11 years@danmanstx I've created the symbolic link in my
/usr/local/bin
directory yet it still gives me the error. I note that my ST3 package has been installed at/Applications/Sublime Text.app/...
and have made the distinction when creating my symbolic link. Yet the error remains - I think it has something to do with my.bash_profile
file. -
Richard Ortega almost 11 yearsI had a problem with ST3 where when I first typed in
subl .
I would get a Sublime editor calleduntitled
and it didn't reference any files in the directory I was in. Doingexport EDITOR='subl -w'
to my.bash_profile
fixed this. -
Giles Butler over 10 yearsThis worked for me but I had to remove the symlink to the old subl file first.
-
dnlcrl over 10 yearsI had to sudo rm /usr/bin/subl to make it work on OSX 10.9, thanks!
-
stevenspiel over 10 yearsthis worked beautifully until the last step. I'm getting
ln: /usr/subl: Permission denied
Thoughts? -
stevenspiel over 10 yearsI think I found the answer here stackoverflow.com/questions/10196449/…
-
merqlove over 10 yearsSame here, but for Bash.
-
Ben Haley over 10 yearsI had to use the home directory, ~, instead of /usr/bin, to make this work for me ... "sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ~/bin/subl"
-
Roy Calderon about 10 yearsThx Michael Keenan, worked great with Sublime Text 3 on Mavericks!
-
Bruno Koga about 10 yearsWorked on Sublime 3 running on Lion (10.7.3) :)
-
Joshua Soileau almost 10 yearsSame here, this worked for me, but I needed to change it to Sublime text 2 with this string: "Sublime\ Text\ 2.app/"
-
Jitendra Vyas almost 10 yearsThis is working but it's not opening the same folded where I ran Subl
-
Sarah Lewis over 9 yearsAs @mr.musicman found, you may need to use
sudo
in front of the command in step 3. -
A F over 9 yearsIf you get
/usr/local/bin/subl: No such file or directory
runmkdir /usr/local/bin
. Your machine is telling you that the/usr/local/bin
folder does not exist, so you can't write to it. -
Reb over 9 yearsWorked on Sublime 2 (with variations as per your note, and using sudo before the command as Sarah points out) on Snow Leopard. Thanks!
-
Christian Gossain over 9 yearsPerfect! This answer did it for me. Thanks!
-
atilkan over 9 yearsThis is for ST 2 i guess. ST 3 comes with CLI but i can't run it yet.
-
Montaro about 9 yearsFor sublime2
sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/bin/subl
-
tmthyjames about 9 yearsAfter trying the other answers, this one was the only one to work for me. Mac OS X Yosemite 10.10.1.
-
Admin almost 9 yearsOne thing that I ran into was I needed to create a bash profile (with: touch ~/.bash_profile). I also did not have a usr/local/bin so I choose usr/local/sbin as the directory (which is the same as bin but requires sudo privileges). If you have permission errors, you should run the command 'sudo' first.
-
mbokil almost 9 yearsIf you faster access you can create an alias. Type nano .bash_profile and paste in: alias subl="open -a 'sublime text'"
-
Kuchi almost 9 yearsWorked on OS X 10.10.4
-
Thomas Bindzus over 8 years@emrah I am using ST3
-
atilkan over 8 years@ThomasBindzus I figured out later. Thanks.
-
Ganatra over 8 years@Tinple I am sorry I haven't tried it on 10.11, I guess the same thing should work
-
Ross over 8 years@Tinple This works in 10.11
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
See the answer here for why - apple.stackexchange.com/questions/196224/… -
Dennis Golomazov over 8 yearsThank you! It's the only solution that worked for me. To make it open in a new tab in the existing window (not in a new one), see stackoverflow.com/questions/9961350/…
-
pjlamb12 about 8 yearsThe big step for me here was the PATH setup. I think my path was off, and that was keeping my symlink from working.
-
Vinodh Thiagarajan about 8 yearsI'm new to Mac and I tried for quite sometime to make it work. Let me tell the reason why other straight forward answers didnt work. While installing Sublime Text for the first time , remember it will ask "Just for you or everyone" , so it is installed in the Applications folder found in your ROOT. Even though it will show under your Applications. Hence this SUDO (Super user Do , played a vital role). First thing Im learning UNIX/LINUX whatever. Wanted to share this learning.
-
CodeBiker about 8 yearsThis did not work for me on Mac OS X El Capitan -- I was unable to copy to file over. Chutipong Roobklom's answer did work, however.
-
Telvin Nguyen about 8 yearsWorked on OSX 10.11.4
-
Michael Conlin almost 8 yearsWorked for me on two separate macs. I appreciate the well written explanation.
-
MageeWorld almost 8 yearsMaybe not everyone knows this - but if you set up the subl link for sublime text 2 (as I did) you either need to remove the existing symbolic link (if you're a mac user like me you know enough to be dangerous but not it all)(
-
Robert Sinclair almost 8 yearsthe only thing that worked after following the instructions, super thorough! Better than the blogpost or the git post I had to waste time on before landing here
-
NicolasKittsteiner almost 8 yearsIn my case the link points to
ln -s /Volumes/Macintosh\ HD/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl
-
Subhajit over 7 years@ChutipongRoobklom Thanks. It worked for me on
macOS Sierra Version: 10.12 (16A323)
. -
organillero over 7 yearsln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
-
vgrinko over 7 yearswhy do we need quotes? it works with quotes for me, but does not work without them?
-
Ken Ratanachai S. over 7 yearsln: /usr/bin/subl: Operation not permitted
-
Josh Foskett over 7 years@KenRatanachaiS. Use
/usr/local/bin/subl
, instead of/usr/bin/subl
. -
Angelfirenze over 7 yearsThis finally did it, thank you so much for posting this! I've been trying to get ST3 to run from the command line for months after the original symlink broke for some reason. Anyway, thank you!
-
Code Cooker about 7 yearsHere is a useful video it will solve your problem to adding sublime with cmd or git bash Open sublime with git or CMD
-
David Rawson about 7 yearsWorking on macOS Sierra 10.12.5. Thanks
-
Alexander Kucheryuk almost 7 yearsCan confirm that works on macOS Sierra 10.12.6 as well. Thank you!
-
talsibony almost 7 yearsmy sublime text path was different I guess it because I am using sublime 2 ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
-
talsibony almost 7 yearsAll you had to do is uncoment the export PATH=$HOME/bin:/usr/local/bin:$PATH in .zshrc and then source ~/.zshrc
-
Dani almost 7 yearsI'm so sorry, I wanted to upvote this, but turns out that (I don't know when) I already did. 🙏
-
flow2k over 6 yearsTLDR:
subl
is in/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl
. -
explorer over 6 yearsit is not fixing for me. I have this set up. ~ > source ~/.bash_profile ~ > echo $EDITOR subl -w ~ > subl . ~ > which subl /Users/vishlpa4/bin/subl ~ > ls -al ~/bin/subl lrwxr-xr-x 1 root staff 62 Oct 8 18:22 /Users/vishlpa4/bin/subl -> /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl...but still sublime pops up with untitled file when I do "sublime filename".
-
cardamom over 6 yearsThanks, this worked, but just running it twice was not enough, had to remove the existing sublime with
sudo rm -rf
on the sublime directory in the applications folder first. -
chichilatte over 6 yearsWorks on High Sierra OSX 10.13.3
-
guice about 6 years^ Use this one. Soft-links into /usr/*local*/bin.
-
guice about 6 yearsSee below, use the one with /usr/*local*/bin. Try not to add custom softlinks into system directories.
-
Reihan_amn about 6 yearsit gives me this: ln: /usr/local/bin/subl: File exists What do I need to do?
-
Reihan_amn about 6 yearsI can see "sub" here: /Applications/Sublime Text.app/Contents/SharedSupport/bin I also can see both "sublime" and "subl" here: /usr/local/bin/ However, it cannot work at all! What could go wrong here?
-
Reihan_amn about 6 yearsNow working! I removed the symbolic path once using rm -f /usr/local/bin/subl and created it again by your comment. Now it is working!
-
Neeraj Jain almost 6 yearsIn 2018 Only Ross's comment works, @Ross Please add your comment as answer it will definitely help others.
-
stackPusher over 5 yearsThis works for me but it opens sublime as a "device", and my mac seems to think its different than opening sublime via the icon because i can get 2 sublimes on my dock. Why do I see sublime appear as an ejectable device when i open it with the command line after adding this to bash_profile??
-
grooveplex about 5 yearsI think this is the cleanest option of all. Thanks!
-
esaruoho almost 5 yearsHow do you modify this so that it will create a file that doesn't actually exist? I know how to use open -a sublimetext to open a file that exists, but if i'm creating a new textfile, what should i modify the open script to?
-
Kent Aguilar over 4 yearsWorks for me! :)
-
Kent Aguilar over 4 years@Reihan_amn - late reply and I think you solved it already, but just as a reference. There seems to be an existing link associated to that name. You can use rm /usr/local/bin/subl then you can perform executing the symbolic link
-
PeeJee over 4 yearsAs of El Capitan's and up, you need to paste the subl file in the /usr/local/bin folder. So follow step 2 to point 6, open it in the terminal and execute the command sudo cp subl /usr/local/bin. This is because of the new System Integrity Protection feature.
-
Danodemo over 4 yearsEveryone should be using oh-my-zsh anyways, so upvoted for visibility!
-
RustyShackleford over 4 yearsWorks on Catalina 10.15
-
skiabox about 4 yearsAwesome!I can confirm that it still works on Catalina 10.15.4
-
arunwithasmile almost 4 yearsI don't understand why we have to create symbolic links n all when this solution is SO MUCH better
-
Abhijeet Khangarot over 3 years2021 update : I'm using Big Sur 11.1, worked perfectly
-
Jyoti Duhan about 3 yearsDidn’t work for me with zsh: command not found: subl
-
Jyoti Duhan about 3 yearsDidn’t work for me and failed with below lines: ln: /usr/local/bin/subl: No such file or directory Please suggest
-
Olivier JM almost 3 yearsWorks on Big Sur 11.4. Thanks
-
Victor Valente almost 3 yearsThis is the most straightforward way to do this.
-
Sam Woolf almost 3 yearsworks on Monterey beta
-
Vyshak over 2 yearsWorked on 11.6.
-
Stickers over 2 years@JyotiDuhan zsh was added above.
-
jgarces over 2 yearsAnd any way for opening several files in the same window? Because each time it opens a new one... (I've already tried this but without success). Thanks
-
meyerson over 2 yearsI think this is the simplest solution
-
meyerson over 2 yearsI think there are ways around this, but as given, this solution will fail when an argument feed to the subl alias is a non-existent file. I fount it more reliable to just extend PATH to include the sublime /path dir as suggested by @Artemiy
-
Mihai over 2 yearsOr:
alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl $*"
-
Deepak Garg over 2 yearsafter adding alias into ~/.bash_profile, do
source ~/.bash_profile
-
Ermiya Eskandary over 2 years2022 update: works fantastic on macOS Monterey 12.2.1 - thank you!
-
Anver Sadhat about 2 yearsexport PATH="$PATH:/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code:/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"