Open Sublime Text from Terminal in macOS

349,322

Solution 1

I finally got this to work on my OSX box. I used these steps to get it to work:

  1. 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 to Sublime\ Text\ 3.app or Sublime\ Text\ 2.app depending upon where the application is stored in your Applications 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!

  2. 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 using nano ~/.bash_profile. These are the following lines that pertain to having subl 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 subl

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

  3. 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 your PATH 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"
Share:
349,322
user1405049
Author by

user1405049

Updated on July 08, 2022

Comments

  • user1405049
    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
    user1405049 about 11 years
    It returns ln: /Users/###/bin/subl: No such file or directory
  • danmanstx
    danmanstx about 11 years
    did 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
    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
    Richard Ortega almost 11 years
    I had a problem with ST3 where when I first typed in subl . I would get a Sublime editor called untitled and it didn't reference any files in the directory I was in. Doing export EDITOR='subl -w' to my .bash_profile fixed this.
  • Giles Butler
    Giles Butler over 10 years
    This worked for me but I had to remove the symlink to the old subl file first.
  • dnlcrl
    dnlcrl over 10 years
    I had to sudo rm /usr/bin/subl to make it work on OSX 10.9, thanks!
  • stevenspiel
    stevenspiel over 10 years
    this worked beautifully until the last step. I'm getting ln: /usr/subl: Permission denied Thoughts?
  • stevenspiel
    stevenspiel over 10 years
    I think I found the answer here stackoverflow.com/questions/10196449/…
  • merqlove
    merqlove over 10 years
    Same here, but for Bash.
  • Ben Haley
    Ben Haley over 10 years
    I 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
    Roy Calderon about 10 years
    Thx Michael Keenan, worked great with Sublime Text 3 on Mavericks!
  • Bruno Koga
    Bruno Koga about 10 years
    Worked on Sublime 3 running on Lion (10.7.3) :)
  • Joshua Soileau
    Joshua Soileau almost 10 years
    Same here, this worked for me, but I needed to change it to Sublime text 2 with this string: "Sublime\ Text\ 2.app/"
  • Jitendra Vyas
    Jitendra Vyas almost 10 years
    This is working but it's not opening the same folded where I ran Subl
  • Sarah Lewis
    Sarah Lewis over 9 years
    As @mr.musicman found, you may need to use sudo in front of the command in step 3.
  • A F
    A F over 9 years
    If you get /usr/local/bin/subl: No such file or directory run mkdir /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
    Reb over 9 years
    Worked 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
    Christian Gossain over 9 years
    Perfect! This answer did it for me. Thanks!
  • atilkan
    atilkan over 9 years
    This is for ST 2 i guess. ST 3 comes with CLI but i can't run it yet.
  • Montaro
    Montaro about 9 years
    For sublime2 sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/bin/subl
  • tmthyjames
    tmthyjames about 9 years
    After trying the other answers, this one was the only one to work for me. Mac OS X Yosemite 10.10.1.
  • Admin
    Admin almost 9 years
    One 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
    mbokil almost 9 years
    If you faster access you can create an alias. Type nano .bash_profile and paste in: alias subl="open -a 'sublime text'"
  • Kuchi
    Kuchi almost 9 years
    Worked on OS X 10.10.4
  • Thomas Bindzus
    Thomas Bindzus over 8 years
    @emrah I am using ST3
  • atilkan
    atilkan over 8 years
    @ThomasBindzus I figured out later. Thanks.
  • Ganatra
    Ganatra over 8 years
    @Tinple I am sorry I haven't tried it on 10.11, I guess the same thing should work
  • Ross
    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
    Dennis Golomazov over 8 years
    Thank 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
    pjlamb12 about 8 years
    The 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
    Vinodh Thiagarajan about 8 years
    I'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
    CodeBiker about 8 years
    This 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
    Telvin Nguyen about 8 years
    Worked on OSX 10.11.4
  • Michael Conlin
    Michael Conlin almost 8 years
    Worked for me on two separate macs. I appreciate the well written explanation.
  • MageeWorld
    MageeWorld almost 8 years
    Maybe 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
    Robert Sinclair almost 8 years
    the 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
    NicolasKittsteiner almost 8 years
    In my case the link points to ln -s /Volumes/Macintosh\ HD/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl
  • Subhajit
    Subhajit over 7 years
    @ChutipongRoobklom Thanks. It worked for me on macOS Sierra Version: 10.12 (16A323).
  • organillero
    organillero over 7 years
    ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
  • vgrinko
    vgrinko over 7 years
    why do we need quotes? it works with quotes for me, but does not work without them?
  • Ken Ratanachai S.
    Ken Ratanachai S. over 7 years
    ln: /usr/bin/subl: Operation not permitted
  • Josh Foskett
    Josh Foskett over 7 years
    @KenRatanachaiS. Use /usr/local/bin/subl, instead of /usr/bin/subl.
  • Angelfirenze
    Angelfirenze over 7 years
    This 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
    Code Cooker about 7 years
    Here 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
    David Rawson about 7 years
    Working on macOS Sierra 10.12.5. Thanks
  • Alexander Kucheryuk
    Alexander Kucheryuk almost 7 years
    Can confirm that works on macOS Sierra 10.12.6 as well. Thank you!
  • talsibony
    talsibony almost 7 years
    my 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
    talsibony almost 7 years
    All you had to do is uncoment the export PATH=$HOME/bin:/usr/local/bin:$PATH in .zshrc and then source ~/.zshrc
  • Dani
    Dani almost 7 years
    I'm so sorry, I wanted to upvote this, but turns out that (I don't know when) I already did. 🙏
  • flow2k
    flow2k over 6 years
    TLDR: subl is in /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl.
  • explorer
    explorer over 6 years
    it 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
    cardamom over 6 years
    Thanks, 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
    chichilatte over 6 years
    Works on High Sierra OSX 10.13.3
  • guice
    guice about 6 years
    ^ Use this one. Soft-links into /usr/*local*/bin.
  • guice
    guice about 6 years
    See below, use the one with /usr/*local*/bin. Try not to add custom softlinks into system directories.
  • Reihan_amn
    Reihan_amn about 6 years
    it gives me this: ln: /usr/local/bin/subl: File exists What do I need to do?
  • Reihan_amn
    Reihan_amn about 6 years
    I 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
    Reihan_amn about 6 years
    Now 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
    Neeraj Jain almost 6 years
    In 2018 Only Ross's comment works, @Ross Please add your comment as answer it will definitely help others.
  • stackPusher
    stackPusher over 5 years
    This 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
    grooveplex about 5 years
    I think this is the cleanest option of all. Thanks!
  • esaruoho
    esaruoho almost 5 years
    How 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
    Kent Aguilar over 4 years
    Works for me! :)
  • Kent Aguilar
    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
    PeeJee over 4 years
    As 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
    Danodemo over 4 years
    Everyone should be using oh-my-zsh anyways, so upvoted for visibility!
  • RustyShackleford
    RustyShackleford over 4 years
    Works on Catalina 10.15
  • skiabox
    skiabox about 4 years
    Awesome!I can confirm that it still works on Catalina 10.15.4
  • arunwithasmile
    arunwithasmile almost 4 years
    I don't understand why we have to create symbolic links n all when this solution is SO MUCH better
  • Abhijeet Khangarot
    Abhijeet Khangarot over 3 years
    2021 update : I'm using Big Sur 11.1, worked perfectly
  • Jyoti Duhan
    Jyoti Duhan about 3 years
    Didn’t work for me with zsh: command not found: subl
  • Jyoti Duhan
    Jyoti Duhan about 3 years
    Didn’t work for me and failed with below lines: ln: /usr/local/bin/subl: No such file or directory Please suggest
  • Olivier JM
    Olivier JM almost 3 years
    Works on Big Sur 11.4. Thanks
  • Victor Valente
    Victor Valente almost 3 years
    This is the most straightforward way to do this.
  • Sam Woolf
    Sam Woolf almost 3 years
    works on Monterey beta
  • Vyshak
    Vyshak over 2 years
    Worked on 11.6.
  • Stickers
    Stickers over 2 years
    @JyotiDuhan zsh was added above.
  • jgarces
    jgarces over 2 years
    And 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
    meyerson over 2 years
    I think this is the simplest solution
  • meyerson
    meyerson over 2 years
    I 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
    Mihai over 2 years
    Or: alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl $*"
  • Deepak Garg
    Deepak Garg over 2 years
    after adding alias into ~/.bash_profile, do source ~/.bash_profile
  • Ermiya Eskandary
    Ermiya Eskandary over 2 years
    2022 update: works fantastic on macOS Monterey 12.2.1 - thank you!
  • Anver Sadhat
    Anver Sadhat about 2 years
    export PATH="$PATH:/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code:/Applications/Subli‌​me\ Text.app/Contents/SharedSupport/bin/subl"