559

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?

Ivar
  • 6,138
  • 12
  • 49
  • 61
user1405049
  • 5,591
  • 3
  • 14
  • 3
  • 4
    How have you gone with this? I'm having problems with `subl` for ST3 too. I have created the symbolic link in `/usr/local/bin/subl` which points to `/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl`. I've even added `export PATH=/usr/local/bin:$PATH` and `export EDITOR="subl -w"` in my `~/.bash_profile` - yet it still doesn't work and I get the same error. – rs77 May 11 '13 at 07:24
  • I'm having a strange problem wherein I have to actually open the subl folder to get the command to execute. The following executes:`Angelfirenze$ /Applications/Sublime\ Text\ 3.app/Contents/SharedSupport/bin/subl ; exit; logout Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. [Process completed]` And Sublime Text 3 finally opens in a new session. I have Homebrew installed and followed the instructions for that. – Angelfirenze May 12 '16 at 01:31
  • 7
    If you want to open the file `file.txt` with Sublime Text from the command line, the command `open -a "Sublime Text" file.txt` may be better suited. – mareoraft Dec 29 '16 at 22:37
  • The command isn't `.subl`, it's `subl`. – the Tin Man Nov 22 '19 at 20:19
  • After modifying `.bash_profile` make sure to close Terminal and re-open it. This is the only thing that really help me here. I wanna make sure that nobody was stuck on this obvious step but really important ^^ – SHANNAX Dec 06 '20 at 10:42

33 Answers33

774

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.

AndreM96
  • 1,355
  • 1
  • 23
  • 34
rs77
  • 8,737
  • 2
  • 19
  • 19
  • 5
    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. – Richard Ortega Aug 08 '13 at 22:58
  • 6
    This worked for me but I had to remove the symlink to the old subl file first. – Giles Butler Sep 30 '13 at 08:33
  • 4
    this worked beautifully until the last step. I'm getting `ln: /usr/subl: Permission denied` Thoughts? – stevenspiel Nov 09 '13 at 14:39
  • 3
    I think I found the answer here http://stackoverflow.com/questions/10196449/installing-sublime-texts-command-line-tool-subl-in-terminal-permission-denie – stevenspiel Nov 09 '13 at 14:40
  • Thx Michael Keenan, worked great with Sublime Text 3 on Mavericks! – Roy Calderon May 10 '14 at 18:52
  • Worked on Sublime 3 running on Lion (10.7.3) :) – Bruno Koga May 24 '14 at 16:10
  • 4
    As @mr.musicman found, you may need to use `sudo` in front of the command in step 3. – Sarah Lewis Oct 16 '14 at 17:11
  • 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. – A F Oct 20 '14 at 20:47
  • Worked on Sublime 2 (with variations as per your note, and using sudo before the command as Sarah points out) on Snow Leopard. Thanks! – Reb Jan 22 '15 at 09:30
  • 1
    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. –  Jul 08 '15 at 18:20
  • The big step for me here was the PATH setup. I think my path was off, and that was keeping my symlink from working. – pjlamb12 Mar 31 '16 at 17:14
  • Worked for me on two separate macs. I appreciate the well written explanation. – Michael Conlin Aug 24 '16 at 16:04
  • 1
    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)( – MageeWorld Aug 30 '16 at 01:07
  • 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 – Robert Sinclair Sep 06 '16 at 00:16
  • Here is a useful video it will solve your problem to adding sublime with cmd or git bash [Open sublime with git or CMD](https://youtu.be/Co9-jPuNvR8) – Code Cooker May 19 '17 at 13:49
  • TLDR: `subl` is in `/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl`. – flow2k Oct 12 '17 at 00:58
  • 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 Apr 18 '18 at 22:25
  • 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! – Reihan_amn Apr 18 '18 at 22:32
  • Just curious - what's the benefit of doing all the sym linking rather than just throwing a function in your .zshrc or .bashrc? For example, `╰─➤ show-function subl subl () { /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl $1 }` – jlarks32 Jul 25 '23 at 12:49
487

For MAC 10.8+:

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

worked.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ganatra
  • 6,498
  • 3
  • 17
  • 16
  • 2
    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" – Ben Haley Feb 20 '14 at 19:57
  • This is working but it's not opening the same folded where I ran Subl – Jitendra Vyas Sep 25 '14 at 15:07
  • 2
    For sublime2 `sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/bin/subl` – Montaro May 20 '15 at 23:08
  • Worked on OS X 10.10.4 – Kuchi Jul 31 '15 at 14:47
  • @Tinple I am sorry I haven't tried it on 10.11, I guess the same thing should work – Ganatra Oct 28 '15 at 18:33
  • 68
    @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 - http://apple.stackexchange.com/questions/196224/unix-ln-s-command-not-permitted-in-osx-el-capitan-beta3 – Ross Nov 12 '15 at 21:05
  • 1
    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. – Vinodh Thiagarajan Apr 28 '16 at 00:48
  • 1
    Worked on OSX 10.11.4 – Telvin Nguyen Jun 02 '16 at 16:35
  • 1
    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". – explorer Oct 17 '17 at 20:58
  • 1
    In 2018 Only Ross's comment works, @Ross Please add your comment as answer it will definitely help others. – Neeraj Jain Aug 27 '18 at 08:45
  • 1
    2021 update : I'm using Big Sur 11.1, worked perfectly – Abhijeet Khangarot Jan 09 '21 at 12:09
  • 1
    Didn’t work for me and failed with below lines: ln: /usr/local/bin/subl: No such file or directory Please suggest – Jyoti Duhan May 15 '21 at 04:23
  • 1
    Worked on 11.6. – Vyshak Oct 14 '21 at 04:29
  • 4
    2022 update: works fantastic on macOS Monterey 12.2.1 - thank you! – Ermiya Eskandary Mar 14 '22 at 16:48
  • 1
    export PATH="$PATH:/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code:/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl" – Anver Sadhat Apr 29 '22 at 16:35
  • 1
    Worked on Monterey 12.3 thank you – Sara Inés Calderón Aug 18 '22 at 16:22
  • 1
    Still working perfectly on MacOS 12.6 (21G115). – JayRizzo Oct 18 '22 at 05:59
  • This worked on macOS Ventura 13.14.1 `sudo ln -s /Applications/Sublime\ Text\ \(4143\).app/Contents/SharedSupport/bin/subl /usr/local/bin/subl` – amarVashishth Aug 02 '23 at 14:51
213

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Chutipong Roobklom
  • 2,953
  • 2
  • 17
  • 20
90

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.

Idan
  • 5,405
  • 7
  • 35
  • 52
Thomas Bindzus
  • 1,508
  • 12
  • 12
70

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"
Roger Sobrado
  • 700
  • 6
  • 7
64

The one I will use is very simple.

open -a "sublime text" [file]

this opens up the sublime text right away. You can specify the file to open as an optional parameter, e.g. to open "myfile.txt" in the current directory.

Adam
  • 55
  • 1
  • 6
user1
  • 645
  • 8
  • 9
  • 5
    If you faster access you can create an alias. Type nano .bash_profile and paste in: alias subl="open -a 'sublime text'" – mbokil Jul 21 '15 at 19:42
  • 1
    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 http://stackoverflow.com/questions/9961350/in-sublime-text-2-how-do-i-open-new-files-in-a-new-tab – Dennis Golomazov Jan 21 '16 at 23:09
  • I think this is the cleanest option of all. Thanks! – grooveplex Apr 21 '19 at 10:59
  • 1
    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? – esaruoho Sep 27 '19 at 09:44
  • I don't understand why we have to create symbolic links n all when this solution is SO MUCH better – arunwithasmile Sep 18 '20 at 08:05
57

You can create a new alias in Terminal:

nano ~/.bash_profile

Copy this line and paste it into the editor:

alias subl='open -a "Sublime Text"'

Hit control + x, then y, then enter to save and close it.

Close all Terminal windows and open it up again.

That's it, you can now use subl filename or subl .

Update for zsh:

Default shell was changed to zsh since macOS 10.15 Catalina.

nano ~/.zshrc

The rest of the steps remain the same.

Yun
  • 3,056
  • 6
  • 9
  • 28
Stickers
  • 75,527
  • 23
  • 147
  • 186
  • Didn’t work for me with zsh: command not found: subl – Jyoti Duhan May 15 '21 at 04:21
  • 10
    This is the most straightforward way to do this. – Victor Valente Sep 03 '21 at 15:14
  • @JyotiDuhan zsh was added above. – Stickers Oct 29 '21 at 18:13
  • 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 – meyerson Dec 10 '21 at 21:07
  • 1
    Or: `alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl $*"` – Mihai Jan 18 '22 at 07:49
  • 3
    after adding alias into ~/.bash_profile, do `source ~/.bash_profile` – Deepak Garg Mar 02 '22 at 14:35
  • After adding the alias in your ~/.bash_profile you can hit esc --> :wq! to force save the changes in MacOS as well if you are in vi mode. Then do source ~/.bash_profile to refresh the environment variables to sync the changes made without having to close and reopen your terminal session. – BV45 Jul 23 '22 at 13:20
28

There is a easy way to do this. It only takes a couple steps and you don't need to use the command line too much. If you new to the command line this is the way to do it.

Step 1 : Finding the bin file to put the subl executable file in

  1. Open up the terminal
  2. type in cd .. ---------------------this should go back a directory
  3. type in ls ------------------------to see a list of files in the directory
  4. type in cd .. ---------------------until you get a folder that contains usr
  5. type in open usr ---------------this should open the finder and you should see some folders
  6. open up the bin folder -------this is where you will copy your sublime executable file.

Step 2: Finding the executable file

  1. open up the finder
  2. Under file open up a new finder window (CMD + N)
  3. Navigate to applications folder
  4. find Sublime Text and right click so you get a pulldown menu
  5. Click on Show Package Content
  6. Open up Content/SharedSupport/bin
  7. Copy the subl file
  8. Paste it in the bin folder in the usr folder we found earlier
  9. In the terminal type in subl --------------this should open Sublime Text

Make sure that it gets copied and it's not a shortcut. If you do have a problem, view the usr/bin folder as icons and paste the subl in a empty area in the folder. It should not have a shortcut arrow in the icon image.

chris Frisina
  • 19,086
  • 22
  • 87
  • 167
bjtran1234
  • 281
  • 4
  • 4
  • After trying the other answers, this one was the only one to work for me. Mac OS X Yosemite 10.10.1. – tmthyjames Jun 03 '15 at 02:33
  • 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. – CodeBiker Apr 29 '16 at 21:14
  • 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. – PeeJee Dec 19 '19 at 18:59
21

Try this.

ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
takasoft
  • 1,228
  • 15
  • 15
17

The Symlink command from the Sublime Text 3 documentation won't work as there is no ~/bin/ directory in Home location on Mac OS X El Capitan or later.

So, we'll need to place the symlink on the /usr/local/bin as this path would be in our $PATH variable in most cases.

So, the following command should do the trick:

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

Once you create the symlink correctly, you would be able to run the Sublime Text 3 like this: subl . (. means the current directory)

Md Mazedul Islam Khan
  • 5,318
  • 4
  • 40
  • 66
16

Just follow the Sublime Documentation

Bash

echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.bash_profile

ZSH

echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.zprofile

Linux

sudo ln -s /opt/sublime_text/sublime_text /usr/local/bin/subl

Make sure you close the terminal after run the command.

Usage

To see the available flags, run subl --help.

Enrique
  • 1,586
  • 17
  • 14
12

Please note not to write into /usr/bin but instead into /usr/local/bin. The first one is for app that write themselves the binary into the system and the latest is for that specific usage of making our own system-wide binaries (which is our case here when symlinking).

Also /usr/local/bin is read after /usr/bin and therefore also a good place to override any default app.

Considering this, the right symlinking would be:

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

vinyll
  • 11,017
  • 2
  • 48
  • 37
12

Close Sublime. Run this command. It will uninstall it. You won't lose your prefs. Then run it again. It will automatically bind subl.

brew install Caskroom/cask/sublime-text
Govind Rai
  • 14,406
  • 9
  • 72
  • 83
  • 2
    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! – Angelfirenze Jan 31 '17 at 23:17
  • 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. – cardamom Jan 16 '18 at 10:10
11

The following worked for me

open -a Sublime\ Text file_name.txt
open -a Sublime\ Text Folder_Path

You can use alias to make it event simple like

Add the following line in your

~/.bash_profile

alias sublime="open -a Sublime\ Text $@"

Then next time you can use following command to open files/folders

sublime file_name.txt
sublime Folder_Path
RB7
  • 435
  • 7
  • 9
  • And any way for opening several files in the same window? Because each time it opens a new one... (I've already tried [this](https://stackoverflow.com/questions/21023529/how-to-open-files-and-folders-in-same-window-in-sublime-text-on-macos) but without success). Thanks – jgarces Nov 12 '21 at 10:19
10

In OS X Mavericks running Sublime Text 2 the following worked for me.

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

Its handy to locate the file in the finder and drag and drop that into the terminal window so you can be sure the path is the correct one, I'm not a huge terminal user so this was more comfortable for me. then you can go to the start of the path and start adding in the other parts like the shorthand UNIX commands. Hope this helps

Jonathan Beech
  • 509
  • 1
  • 6
  • 16
10

It works !!!! for me on MacOS Sierra 10.12.2

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

and find it in terminal subl

tarikul05
  • 1,843
  • 1
  • 16
  • 24
9

Adding this to ur .bashrc or .zshrc is a simple solution.

alias sublime="open -a /Applications/Sublime\ Text.app"
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
Won Jun Bae
  • 5,140
  • 6
  • 43
  • 49
9

Starting with macOS Catalina (ver 10.15) zsh is used. To allow Sublime to run from the command line, edit .zshrc in your home directory (cd with no arguments will get you there) and add the following line:

export PATH="$PATH:/Applications/Sublime Text.app/Contents/SharedSupport/bin"

To run Sublime Text from the command line use subl [filename]

Rahav
  • 1,755
  • 15
  • 18
7

if you have subl set up to be called from the command line, the proper command to open the current directory is:

subl .

"OS X Command Line" is a link on how to make sure everything is set up.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
danmanstx
  • 1,692
  • 2
  • 15
  • 21
  • It returns `ln: /Users/###/bin/subl: No such file or directory` – user1405049 Apr 26 '13 at 04:05
  • 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` – danmanstx Apr 29 '13 at 13:51
  • @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. – rs77 May 11 '13 at 07:28
  • In my case the link points to ```ln -s /Volumes/Macintosh\ HD/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ``` – NicolasKittsteiner Sep 15 '16 at 15:05
7

You shouldn't pollute /usr/bin directory unless you really need to. I always use /usr/local/bin for those binaries that aren't managed by the distribution package manager. Why? Because if the package manager gets updated it'll always replace the files in /usr/bin.

So what I'd do is

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

tonirilix
  • 81
  • 1
  • 1
7

You can just add an alias

alias subl='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'

Then you should be able to open a folder or whatever with

subl <path>
zakfisher
  • 71
  • 1
  • 2
7

According to Sublime setup documentation

echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.zprofile

Works fine.

Artemiy
  • 2,702
  • 1
  • 19
  • 13
5

This answer is for Mac OS Ventura and Sublime Text 4 (build 4143) specifically, which breaks the usual method of symlinking <APP_PATH>/bin/subl to /usr/local/bin.

A clean and simple solution to this is to use alias-ing. From any relevant files like: ~/.zshrc or ~/.zprofile add:

alias subl="open -a /Applications/Sublime\ Text.app/Contents/MacOS/sublime_text"

Then, source ~/.zshrc.

The command subl should work.

John Doe
  • 2,173
  • 1
  • 21
  • 12
4

This is to get it working as an ALIAS, not a Symbolic link!

This will allow you to run additional commands in the terminal without interrupting the subl session. Using many of the symbolic link answers here (ln -s), cause the terminal process to endure while using Sublime text. If you want the separation, create an alias in the Bash profile like so:

  1. Test subl from your ST installation:

    First, navigate to a 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 and update ".bash_profile":

    Now add the alias in your Bash Profile. Open it via vim ~/.bash_profile. These are the following lines that pertain to having subl work on the command line for Sublime Text:

    ## For Sublime Text 3 alias
    alias subl='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
    ## For Sublime Text global editor preference **Optional
    export EDITOR='subl -w'
    

    I suggest always commenting your code in here with ##. 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: ST3 subl or ST2 subl

    If you do make any edits to this file once you have closed it, you need to source this file in your current session or close the terminal (tab) and open a new one. You can source this file by running the command source ~/.bash_profile Resolve any errors before moving to the final step.

chris Frisina
  • 19,086
  • 22
  • 87
  • 167
3

For anyone looking for opening a file with Sublime on mac from terminal

open 'path/file.txt' -a '/Applications/Sublime Text.app'
zeeawan
  • 6,667
  • 2
  • 50
  • 56
2

I would add that if you are upgrading from Sublime Text 2, go into /usr/bin and delete your old subl first before following the same instructions above. It's worth the upgrade.

Vincent
  • 1,454
  • 2
  • 17
  • 24
2

I achieve this with just one line in terminal (with Sublime 3):

alias subl='/usr/local/bin/sublime'
Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
2

I am using mac airbook open your terminal and type

 sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/bin/subl

Then type simple subl and file name

 subl index.py
Siddharth Shukla
  • 1,063
  • 15
  • 14
  • See below, use the one with /usr/*local*/bin. Try not to add custom softlinks into system directories. – guice Apr 16 '18 at 18:12
1

I am using Oh-My-Zshell and the previous aliases stated didn't work for me so I wrote a simple bash function that will allow you to open Sublime from the command line by using sublime to open the current folder in the editor. With the addition functionality to specify a file to open the editor from.

# Open Sublime from current folder or specified folder
sublime(){
  /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ./$1
}

Usage to open current folder in terminal:

$ sublime

Usage to open specific folder:

$ sublime path/to/the/file/to/open
Andrew Steinmetz
  • 1,010
  • 8
  • 16
1

Summarizing the different ways you can accomplish it:

  1. To open sublime text from terminal
open /Applications/Sublime\ Text.app/
  1. To open a specific file in current path (or provide path to the file you need to open) using sublime text
open -a /Applications/Sublime\ Text.app/ myFileToOpen.txt
  1. Make your command short by introducing a new alias named 'sublime' and use it

    a. open bash_profile:

    nano ~/.bash_profile

    b. copy this line to create the alias and save and restart terminal

    alias sublime="open -a /Applications/Sublime\ Text.app"

    c. usage: apple.txt will open with sublime text (provide file path if necessary)

    sublime apple.txt

user0904
  • 210
  • 3
  • 7
0

Creating the file in the Default path did not work for me as the Menu.sublime-menu file has overridden almost all other menu options and left me with only the custom one.

What worked for me is creating the below file in path ~/Library/Application Support/Sublime Text 3/Packages/User/Main.sublime-menu (note directory User instead of Default):

[
    {
        "caption": "File",
        "mnemonic": "F",
        "id": "file",
        "children":
        [
            {
                "caption": "Open Recent More",
                "children":
                [
                    { "command": "open_recent_file", "args": {"index": 1 } },
                    { "command": "open_recent_file", "args": {"index": 2 } },
                    { "command": "open_recent_file", "args": {"index": 3 } },
                    { "command": "open_recent_file", "args": {"index": 4 } },
                    { "command": "open_recent_file", "args": {"index": 5 } },
                    { "command": "open_recent_file", "args": {"index": 6 } },
                    { "command": "open_recent_file", "args": {"index": 7 } },
                    { "command": "open_recent_file", "args": {"index": 8 } },
                    { "command": "open_recent_file", "args": {"index": 9 } },
                    { "command": "open_recent_file", "args": {"index": 10 } },
                    { "command": "open_recent_file", "args": {"index": 11 } },
                    { "command": "open_recent_file", "args": {"index": 12 } },
                    { "command": "open_recent_file", "args": {"index": 13 } },
                    { "command": "open_recent_file", "args": {"index": 14 } },
                    { "command": "open_recent_file", "args": {"index": 15 } },
                    { "command": "open_recent_file", "args": {"index": 16 } },
                    { "command": "open_recent_file", "args": {"index": 17 } },
                    { "command": "open_recent_file", "args": {"index": 18 } },
                    { "command": "open_recent_file", "args": {"index": 19 } },
                    { "command": "open_recent_file", "args": {"index": 20 } },
                    { "command": "open_recent_file", "args": {"index": 21 } },
                    { "command": "open_recent_file", "args": {"index": 22 } },
                    { "command": "open_recent_file", "args": {"index": 23 } },
                    { "command": "open_recent_file", "args": {"index": 24 } },
                    { "command": "open_recent_file", "args": {"index": 25 } },
                    { "command": "open_recent_file", "args": {"index": 26 } },
                    { "command": "open_recent_file", "args": {"index": 27 } },
                    { "command": "open_recent_file", "args": {"index": 28 } },
                    { "command": "open_recent_file", "args": {"index": 29 } },
                    { "command": "open_recent_file", "args": {"index": 30 } },
                    { "command": "open_recent_file", "args": {"index": 31 } },
                    { "command": "open_recent_file", "args": {"index": 32 } },
                    { "command": "open_recent_file", "args": {"index": 33 } },
                    { "command": "open_recent_file", "args": {"index": 34 } },
                    { "command": "open_recent_file", "args": {"index": 35 } },
                    { "command": "open_recent_file", "args": {"index": 36 } },
                    { "command": "open_recent_file", "args": {"index": 37 } },
                    { "command": "open_recent_file", "args": {"index": 38 } },
                    { "command": "open_recent_file", "args": {"index": 39 } },
                    { "command": "open_recent_file", "args": {"index": 40 } },
                    { "command": "open_recent_file", "args": {"index": 41 } },
                    { "command": "open_recent_file", "args": {"index": 42 } },
                    { "command": "open_recent_file", "args": {"index": 43 } },
                    { "command": "open_recent_file", "args": {"index": 44 } },
                    { "command": "open_recent_file", "args": {"index": 45 } },
                    { "command": "open_recent_file", "args": {"index": 46 } },
                    { "command": "open_recent_file", "args": {"index": 47 } },
                    { "command": "open_recent_file", "args": {"index": 48 } },
                    { "command": "open_recent_file", "args": {"index": 49 } },
                    { "command": "open_recent_file", "args": {"index": 50 } }
                ]
            }
        ]
    }
]

Result:

(needed to blur some parts of the image for security reasons)

open-recent-more.png

bobasti
  • 1,778
  • 1
  • 20
  • 28
0

For Sublime 4+ it's even simpler with Bash:

echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.bash_profile
Consta Gorgan
  • 549
  • 6
  • 22
-1

I'm on a mac and this worked for me:

open /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl
user9199553
  • 37
  • 1
  • 4