58

I want to apologize in advance that this is a newbie question! I've spent the last 2 hours trying to find a solution.

I have two problems (I'm sure related).

Background:

This is what my $PATH looks like:

/Users/Sponsi/.rvm/gems/ruby-1.9.3-p194/bin:/Users/Sponsi/.rvm/gems/ruby-1.9.3-p194@global/bin:/Users/Sponsi/.rvm/rubies/ruby-1.9.3-p194/bin:/Users/Sponsi/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin

Problem #1

I am trying to use the command-line command "subl" to launch Sublime Text 2 on OSX.

I entered the following command: "ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

It only halfway worked - when I enter "subl" I get "Command not found". But when I re-enter the command above it says "Already exists."

I searched online and found a (somewhat) fix. I added the following to my .bashrc:

echo 'export PATH="./bin:$PATH"' >> ~/.bashrc 
source ~/.bashrc

Using the command "subl" does work but only temporarily. If I exit terminal or switch to another directory it stops working (bringing me into problem #2, see below.)

Problem # 2:

When I try to pull up a file under another directory using the command "subl" I get "-bash: ./bin/subl: No such file or directory" I confirmed the file I want to edit does exist.

BTW, I am following the Ruby tutorial found @ http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Thank you so much for your time!

Jeremy
  • 22,188
  • 4
  • 68
  • 81
Ralitza
  • 741
  • 2
  • 7
  • 12

14 Answers14

156

Run each command in the Terminal (in this order):

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

This should work!

Note: First you should check that your sublime's app is in Applications folder and it's name is Sublime Text 2, if not, you should change the name in the second command and type the name of sublime's app.

MKB
  • 7,587
  • 9
  • 45
  • 71
isanjosgon
  • 1,829
  • 1
  • 13
  • 12
94

if you are using RVM, do this:

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/.rvm/bin/subl
Bob Walsh
  • 13,011
  • 4
  • 24
  • 21
34

For El Captain or later OS:

In El Capitan, you are not allowed to write to /usr/bin, but writing to /usr/local/bin is ok. By default, /usr/local/bin should also be in your path variable.

For Sublime Text 3

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

For Sublime Text 2

sudo rm /usr/local/bin/subl
sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
Rahul Arora
  • 4,503
  • 1
  • 16
  • 24
  • 1
    I've had trouble with this all day, this was the only thing thats worked. thank you! – Gil Oct 27 '19 at 01:30
25

I stuck mine in /usr/bin

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

or for Sublime 3 or later

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

It's already in $PATH

Rimian
  • 36,864
  • 16
  • 117
  • 117
  • 2
    This is the best way to go if you have /usr/bin already. Picks it up automatically without any other fidgeting. – Brian Fegter Mar 07 '13 at 18:19
  • 5
    For Sublime Text 3 (and later) just leave off the "\ 2". Like this: `sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/bin` – Stan James Aug 16 '13 at 15:27
  • 2
    I get operation not permitted when I do this. Any ideas? – jamesscaggs May 06 '16 at 18:43
  • Could be a permissions thing or a corrupt file. Hard to know. – Rimian May 07 '16 at 00:01
  • 2
    [Per this answer](https://stackoverflow.com/a/36734569/1608016), it has to do with El Capitan's new System Integrity Protection feature. I was running into the same issue, and changing the location of the link to `/usr/local/bin` solved the problem for me. – Brice Oct 23 '17 at 17:57
13

If you're using Sublime Text 3 you need to adjust the commands in isanjosgon's answer:

$ sudo rm -rf /usr/local/bin/subl
$ sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/
$ subl .
Naguib Ihab
  • 4,259
  • 7
  • 44
  • 80
7

I had the same problem, on a new Mac recently configured. I had RVM installed before oh-my-zsh and sublime text and I was running into the same problems. I tried different locations on the path

/usr/bin
/usr/local/bin

all of them in the path. What worked for me was to get rid of the quotes on the command line running as

sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/bin/subl
nuin
  • 607
  • 1
  • 6
  • 11
  • 1
    This is what fixed it for me too. – Kieran Andrews May 27 '14 at 00:24
  • The Sublime Text documentation https://www.sublimetext.com/docs/2/osx_command_line.html has quotes around the /Applications.... path. You are quite right that leaving the quotes off is the solution. To see your path type 'echo $PATH'. /usr/local/bin is the best place to put the symlink and should already be in your path. – WorkingMatt Jan 21 '15 at 18:06
4
$ alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"

$ subl . This works! :D

Raj
  • 3,637
  • 8
  • 29
  • 52
3

You can also create new alias if you want like

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

Pankaj Agrawal
  • 1,469
  • 2
  • 17
  • 27
2

Edit your ~/.bashrc file to contain this line at the end of it:

export PATH="$HOME/bin:$PATH

Notice the ~ is there instead of the . from your example? The ~ signifies your home directory, which is where your .bashrc file lives. The dot signifies the current directory of your terminal window.

Once you've made the change, close your terminal window and re-open it. Then subl should be avaiable on the PATH.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • 1
    I'm confused. Now, all I'm receiving is "Command not found". I've restarted my computer, reinstalled Sublime Text, deleted and re-added the subl file in my /bin, and reentered all the commands above to no avail. The only change I did to my computer was delete a user account - that shouldn't have anything to do with subl not working, right? – Ralitza Jun 08 '12 at 19:54
  • Perhaps use `export PATH="$HOME/bin:$PATH` instead? I probably should have said that in the first place. You shouldn't have to delete your program and re-install it. This is purely a configuration issue external from the program itself. – Jeremy Jun 08 '12 at 19:57
2

Regarding your first problem, could it be that you don't have a bin directory set up under you home directory (i.e., ~/bin)? To check this, type the following:

cd ~/
ls -l

If there is no bin directory, type the following:

mkdir bin

Once you've created the directory, retype the following:

subl --help

Hopefully that takes care of the issue

Sean Crockett
  • 96
  • 1
  • 3
1

There's a slight change to the path from that in the accepted answer following an update to Sublime Text.

ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/.rvm/bin/subl

0

The reason that when you changed directory or re-opened terminal then the PATH wouldn't work is that .bashrc is a non-login interactive file.

Simply use this line in terminal:

echo 'export PATH=$PATH:~/bin' >> ~/.bash_profile

will probably solve the problem since .bash_profile is loaded every time there is a login.

if you don't have .bash_profile, just before using the above command, type this in Terminal:

cd ~
touch .bash_profile

then use the formal command.

I would not touch usr/local/bin usually since it is a system level folder. Heard that recently in OS X El Capitan, Apple made some permission restrictions. So just put the clone in your home directory under /bin folder and adding the PATH in your .bash_profile, everything should then be working.

0

In linux sublime 3, in your terminal paste

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

Then

subl3 [filename]
Vimm Rana
  • 86
  • 2
  • 10
0

I first checked the which subl returned nothing. That meant have to add path of subl to PATH variable required.

So adding to PATH variable path of subl helped

PATH=$PATH:/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/

You may first want to find correct path of your subl and add that to PATH variable

If that works, add the path permanently by adding below command to your .bash_profle

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

Note: path to subl may differ in your system.

Lokesh Purohit
  • 523
  • 6
  • 9