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

@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