Skip to content

Instantly share code, notes, and snippets.

@adrianorsouza
Last active September 26, 2023 16:26
Show Gist options
  • Save adrianorsouza/df4759b0583dcd112da4 to your computer and use it in GitHub Desktop.
Save adrianorsouza/df4759b0583dcd112da4 to your computer and use it in GitHub Desktop.
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

@xwartz
Copy link

xwartz commented Nov 25, 2015

Good job!

@joeltmiller
Copy link

Thanks for this

@janeygcode
Copy link

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
Copy link
Author

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
Copy link

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
Copy link
Author

@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
Copy link

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
Copy link

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
Copy link

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
Copy link
Author

@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
Copy link
Author

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
Copy link

airbr commented Feb 19, 2017

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

@sanxinsiyi
Copy link

Very helpful!

@Mawusi
Copy link

Mawusi commented Jun 9, 2017

Hello, can it work on windows as well?

@adrianorsouza
Copy link
Author

@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
Copy link

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

@escpeartst
Copy link

Xie xie ni

@pkayokay
Copy link

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

https://brew.sh/

@kuesugi
Copy link

kuesugi commented May 29, 2018

thank you!

@ambulatorymerperson
Copy link

thank you!

@B747-Raj
Copy link

B747-Raj commented Nov 6, 2019

Worked.
Thanks.

@kanikash4
Copy link

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

It helped straight forward. Thanks!

@pythonloveme
Copy link

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

@adrianorsouza
Copy link
Author

@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
Copy link

Thanks a lot

@ProVisionDevs
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment