Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
launch sublime text from the command line

Launch Sublime Text from the command line on OSX

Sublime Text includes a command line tool, subl, to work with files on the command line. This can be used to open files and projects in Sublime Text, as well working as an EDITOR for unix tools, such as git and subversion.

Requirements

  • Sublime text 2 or 3 installed in your system within Applications folder

Setup

In order to launch sublime from command line you only need to create a symlink /usr/local/bin/subl point to sublime app, to do so run the following in the command line.

Sublime Text 2

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

Sublime Text 3

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

Test it out

open a new file from the command line:

subl test.rb

it should open new file test.rb in Sublime Text

open a project folder

subl dir/project

to launch Sublime app

subl

for more detailed options use the help:

subl -h

Config git to use Sublime Text as its editor globally

run the command as follows:

git config --global core.editor "subl -w"

to config git to use Sublime Text as editor only for a particular repository/project run the same command above without --global option.


NOTE: In order to execute Sublime from the Command Line you must:

have created a directory where you actually place binaries /usr/local/bin if not make it before creating a symlink:

mkdir -p /usr/local/bin

have /usr/local/bin in your PATH environment variable, if not add by running the following command:

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

then reload the shell:

source ~/.bash_profile

test again.

Further info read from sublime docs

@obliviga

This comment has been minimized.

Copy link

@obliviga obliviga commented Aug 18, 2015

This is awesome.

@Lidofernandez

This comment has been minimized.

Copy link

@Lidofernandez Lidofernandez commented Oct 21, 2015

awesome explanation. Thanks!

@xwartz

This comment has been minimized.

Copy link

@xwartz xwartz commented Nov 25, 2015

Good job!

@joeltmiller

This comment has been minimized.

Copy link

@joeltmiller joeltmiller commented Nov 27, 2015

Thanks for this

@janeygcode

This comment has been minimized.

Copy link

@janeygcode janeygcode commented Dec 14, 2015

I can't seem to get this to work :( Could it be because I have El Capitan OS? I tried all the steps but still get "command not found" when typing subl in the command line.

@adrianorsouza

This comment has been minimized.

Copy link
Owner Author

@adrianorsouza adrianorsouza commented Jan 5, 2016

Sorry @janeygak I haven't tested on El Capitan OS, I'm just about to migrate from Mavericks to El Capitan OS, after I test it out I updated here.

UPDATE:

@janeygak it's a bit late but, I've test it out on El Capitan, and yet it works flawlessly for both ST2 and ST3 ;).

So if you still have a trouble, I guess you may check on of the following steps:

1: Check if the subl command does exists is in your PATH, type in terminal the following command:

$ which subl

You should see something like: /usr/local/bin/subl, if the output is empty you probably haven't to created symlink.

2: Make sure the bin folder from the result above is in your PATH:

$ echo $PATH | grep -o /usr/local/bin

You should see in the output: /usr/local/bin, if not, follow the note described at bottom of the gist.

  1. Lately check if you've created the symlink within the bin folder and the symlink point to the subl properly.
$ ls -l /usr/local/bin/

Should see in the output: subl -> /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl

If none of these works let me know.

@gseidel

This comment has been minimized.

Copy link

@gseidel gseidel commented Jan 31, 2016

@janeygak
I've got same problem in El Capitan

bash: /Applications/Sublime: No such file or directory

It seem that El Captan don't like whitespaces in symlink paths. I also try it with backslashes, but it won't help. If i executed from terminal it works. So i use alias now.

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

Just save it in ~/.profile, so it will be executed every time you open a terminal.

@adrianorsouza

This comment has been minimized.

Copy link
Owner Author

@adrianorsouza adrianorsouza commented Mar 10, 2016

@gseidel your approach also works fine as an alias, I think that error was maybe you've tried to create a symlink with backslash or without enclosed quotation mark.

Make sure if you type the command in terminal as follows, enclosed with double quotation mark and without backslash in it:

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

I just tested it out on El Capitan and worked fine, I also updated the previous comment that might help you too.

@AlexBai1991

This comment has been minimized.

Copy link

@AlexBai1991 AlexBai1991 commented Apr 16, 2016

I see why "sublime: command not found" happens......

check /Applications/Sublime Text.app/xxx and ln -sv "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl.
and make sure 'Sublime Text.app' is same.

@dbspin

This comment has been minimized.

Copy link

@dbspin dbspin commented May 14, 2016

Hey I'm having the same problem in OSX 10.11.4.

Sublime symlink sets up fine.

subl

in bash launches Sublime 3 fine.

However, I've tried adding it to git's configuration any number of ways - and none work. Continually get the following error, wheather .gitconfig links directly to the Sublime subl file or to the symlink.

'subl -n -w': subl -n -w: command not found
error: There was a problem with the editor ''subl -n -w''.

Here's the pertinent line from my current git config, as I say, I've tried linking to the file directly also.

core.editor='subl -n -w'

And here's the output of running the ln - sv command above

/usr/local/bin/subl. -> /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl

@sandraqu

This comment has been minimized.

Copy link

@sandraqu sandraqu commented May 24, 2016

Nevermind! @gseidel provided an answer that works. I added the alias line to .bash_profile.

I have Sublime installed in my Mac's Applications directory (not in my user Applications directory).

~/Applications/Sublime\ Text\ 3.app does not exist. Whereas ./Applications/Sublime\ Text\ 3.app does exist.
I tried the symlink as

ln -sv "./Applications/Sublime\ Text\ 3.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime

but sublime . does not work. -bash: sublime: command not found

ls -l /usr/local/bin renders the following
sublime -> ./Applications/Sublime\ Text\ 3.app/Contents/SharedSupport/bin/subl

@adrianorsouza

This comment has been minimized.

Copy link
Owner Author

@adrianorsouza adrianorsouza commented Jul 19, 2016

@dbspin to define subl as your default git editor it's pretty easy, make sure if you got the proper setup within the ~/.gitconfig as follows:

[core]
    editor = subl -w
@adrianorsouza

This comment has been minimized.

Copy link
Owner Author

@adrianorsouza adrianorsouza commented Jul 19, 2016

I've seen so many people here having trouble when creating the symbolic link and use the subl command, as $PATH and different versions within the installation folder may differ from one and other, so here is the simplest way to set up the symlink without having problem with quotation, versions, path etc.

  1. Open Finder, locate your Sublime Text app within the Applications, in general /Applications.
  2. Right click on the App, then Show Package Contents.
  3. Navigate up to: Contents -> SharedSupport -> bin, look for subl file.
  4. Copy this file subl.
  5. Paste the value from clipboard into terminal, it'll give you the right path to the subl command already quoted, use this value to make the symlink as follows.
ln -svf {PASTE RIGHT HERE} /usr/local/bin/subl

Hope this can help others who might be facing the same problem.

@airbr

This comment has been minimized.

Copy link

@airbr airbr commented Feb 19, 2017

@adrianorsouza Thank you for that, helped me after a time machine backup to a new machine.

@sanxinsiyi

This comment has been minimized.

Copy link

@sanxinsiyi sanxinsiyi commented Feb 20, 2017

Very helpful!

@Mawusi

This comment has been minimized.

Copy link

@Mawusi Mawusi commented Jun 9, 2017

Hello, can it work on windows as well?

@adrianorsouza

This comment has been minimized.

Copy link
Owner Author

@adrianorsouza adrianorsouza commented Jun 17, 2017

@Mawusi I'm not sure, it may work on Windows with a different approach here is an example: https://stackoverflow.com/a/25577833/2845262.

@SashaLah

This comment has been minimized.

Copy link

@SashaLah SashaLah commented Jun 17, 2017

@adrianorsouza THank you! finally got it working. I think I also had it in .Applications and not /Applications

@escpeartst

This comment has been minimized.

Copy link

@escpeartst escpeartst commented Aug 3, 2017

Xie xie ni

@pkayokay

This comment has been minimized.

Copy link

@pkayokay pkayokay commented Feb 23, 2018

for all of you having trouble with this... installing wget from homebrew will automatically add subl.

https://brew.sh/

@kuesugi

This comment has been minimized.

Copy link

@kuesugi kuesugi commented May 29, 2018

thank you!

@ambulatorymerperson

This comment has been minimized.

Copy link

@ambulatorymerperson ambulatorymerperson commented Dec 19, 2018

thank you!

@B747-Raj

This comment has been minimized.

Copy link

@B747-Raj B747-Raj commented Nov 6, 2019

Worked.
Thanks.

@kanikash4

This comment has been minimized.

Copy link

@kanikash4 kanikash4 commented Mar 13, 2020

ln -svf {PASTE RIGHT HERE} /usr/local/bin/subl

It helped straight forward. Thanks!

@pythonloveme

This comment has been minimized.

Copy link

@pythonloveme pythonloveme commented Mar 30, 2020

i am getting this error ln: /usr/local/bin/subl: Permission denied

@adrianorsouza

This comment has been minimized.

Copy link
Owner Author

@adrianorsouza adrianorsouza commented Mar 30, 2020

@pythonloveme since macOS High Sierra I guess, the path for /usr/local is not owned by your user, with that in mind since then you have to use sudo to operate with files within that folder or simply change the owner for your bin folder like so:

sudo chown $USER /usr/local/bin
@aminulislam-ai

This comment has been minimized.

Copy link

@aminulislam-ai aminulislam-ai commented Aug 18, 2020

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.