Open a folder in Sublime Text 3 using command line

59,778

Solution 1

Mac Or Linux Only

The best & safest way to do this is to create a symbolic link from the Sublime executable file (subl) to a folder already in your $PATH (e.g. /usr/local/bin/). If you do this; you won't have to update this every time sublime updates...

For users running BASH (i.e. most people):

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

If that doesn't work, create a bin folder in your home directory (if one does not already exist), add it to your PATH variable and create a soft link to that file).

mkdir $HOME/bin
export PATH=$HOME/bin:$PATH
ln -s '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl' $HOME/bin/subl


Then before you start using it properly, I would suggest taking a look at the help text first, which explains it's usage:

 subl -h

e.g.

subl my_folder_name/filename.txt
subl my_folder_name

to open a file and folder in Sublime respectively.


Taking it a step further

I use a BASH function to take this a step further with the following benefits:

  • shorten the shortcut to just s (which is somewhat shorter than subl).
  • automatically open the current directory that you are if no file/directory is specified after subl / s.

If you want, you can use this function by running the following (after running the above):

cd
subl .bashrc

This should open the .bashrc file in Sublime Text. Add the following to the bottom.

function s {
  if [ "$1" != "" ]; then
    subl $1
  else
    subl $PWD
  fi
}   

Then you can open Sublime by simply typing in a s (all the sublime arguments still work)...

(Side Point, I also use a similar function for open (for mac) / or xdg-open (for ubuntu); where I shorten the command to just o. I use it a lot to open the current directory in the file manager)...


Fish Shell Users (you know who you are)

The export line above will not work; so exchange it for the following

set PATH $HOME/bin:$PATH


Before Edit

I had different versions of the command line subl and sublime text three installed. I simply removed the subl command and then re-added and that fixed the problem for me...

For those who may find this useful - this is what I did:

 subl -v

This showed me the build of the command-line sublime, when I checked this against the version of my actual Sublime, I noticed that the command line subl was an older build. So I tried to find the location of the command line subl using the following command (for me this was /usr/bin/subl):

which subl

So I first removed this older command-line sublime text.

sudo rm /usr/bin/subl   (use `sudo` only if necessary)

And then re-added Subl to my PATH (as above)

Solution 2

To open sublime in the same folder you can simply type in your commandline:

subl . 

In order to work you must configure some stuff:

1) To prevent the opening of previous projects you should set the following properties of your Sublime User Settings:

"hot_exit": false,
"remember_open_files": false

2) In order to use subl.exe from anywhere you should add the Sublime folder in the environment variables. I.e. C:\Program Files\Sublime Text 3

Solution 3

That's because by default the side bar does not show, you can show the side bar by

View > Side Bar> Show Side Bar

[enter image description here1

Solution 4

I've had this issue before, on both Mac OSX and Windows, and I found some oddities with it;

Mac OSX You either have to have Sublime Text open already for the subl ./folder_name command to actually open the folder, or Sublime must have been quit with windows still open - if you close all the windows then quit Sublime, using the subl ./folder_name command will just open a blank Sublime window.

Windows You have to have Sublime open for the subl ./folder_name to work. Without Sublime open, it will just open a blank Sublime window.

I've yet to find a way of the command opening fine, no matter how you quit Sublime / when you have Sublime closed.

Try having Sublime open whilst you run the command, and see if it works then.

Solution 5

I was having trouble opening sublime text 3 with sublime text 2 currently installed. To fix this issue:

1) open /usr/local/bin from terminal.

2) locate and delete subl within bin folder

3) copy and pasted '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl' /usr/local/bin/subl into terminal.

4) locate new subl within bin folder

5) used subl in terminal to verify command opens sublime text 3 properly.

6) used subl -v and got Sublime Text Build 3083

Share:
59,778
Ismail Moghul
Author by

Ismail Moghul

I'm a Bioinformatics Researcher...

Updated on July 09, 2022

Comments

  • Ismail Moghul
    Ismail Moghul almost 2 years

    I'm trying to open a directory in sublime Text 3.

    I can launch sublime from the command line using the subl command.

    The help text show the following:

    Sublime Text build 3059
    
    Usage: subl [arguments] [files]         edit the given files
       or: subl [arguments] [directories]   open the given directories
       or: subl [arguments] -               edit stdin
    
    Arguments:
      --project <project>: Load the given project
      --command <command>: Run the given command
      -n or --new-window:  Open a new window
      -a or --add:         Add folders to the current window
      -w or --wait:        Wait for the files to be closed before returning
      -b or --background:  Don't activate the application
      -s or --stay:        Keep the application activated after closing the file
      -h or --help:        Show help (this message) and exit
      -v or --version:     Show version and exit
    
    --wait is implied if reading from stdin. Use --stay to not switch back
    to the terminal when a file is closed (only relevant if waiting for a file).
    
    Filenames may be given a :line or :line:column suffix to open at a specific
    location.
    

    Thus to open a directory I should be able to use the following

    subl ./folder_name

    but that does not work for me. Sublime does open (with a empty new document) and I cannot see the folder in the side bar.

    Am I doing it wrong...

    BTW. I'm using the fish shell with the 'Oh my fish' Add-on (I have also added the sublime add- on)...

  • olimortimer
    olimortimer over 9 years
    @IsmailM mine are all the same version number unfortunately
  • Paul Irish
    Paul Irish over 9 years
    useful! @olimortimer I think this gets us to where we want it: { "hot_exit": false, "remember_open_files": false}
  • Olivier Lacan
    Olivier Lacan about 9 years
    I'd disagree with your advice to add the full path to subl to the $PATH. That's a lot of noise and if you did that for every little executable your path would be a mess. There are executable directories and that's why I recommend symlinking to subl from one of them, say /usr/local/bin on OS X. So ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl. Rest of the explanation here: olivierlacan.com/posts/…
  • Jay
    Jay over 7 years
    "For users running BASH (i.e. most people):" - what if you're not running bash? I use fish, and symlinking subl results in just opening a blank new document like the OP describes. When I execute the same symlink from bash it works as you described.
  • Mohammad Kanan
    Mohammad Kanan about 6 years
    Your answer does not seem to relate to the question .. can you explain what side bar you mentioned?
  • Ivan Li
    Ivan Li about 6 years
    I met the same problem while trying to open a folder via subl, but it does not show the opened folder, finally, I find out that's because sublime text does not show side bar by default, that's why it confuse me that the folder is not opened, when I enable the Show Side Bar shows above, I can see the opened folder .
  • SilasOtoko
    SilasOtoko over 5 years
    This turned out to be my problem too. (Palm to face)