Open a folder in Sublime Text 3 using command line

可紊 提交于 2019-11-29 19:58:08

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)

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

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.

Ivan Li

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

View > Side Bar> Show Side Bar

[

1

To open a folder as a project in Sublime Text, use subl . while in the folder you're trying to open.

Linux So if you want to open ~/Documents/folder_name, then move to that folder in Terminal cd Documents/folder_name and type the command subl .

Note This was only tested in Ubuntu with Sublime Text 2.

Edit Answer found here: http://olivierlacan.com/posts/launch-sublime-text-3-from-the-command-line/

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

While there are already multiple answers and I apologize for adding to the noise, I don't understand why you're using subl ./folder_name to open a local directory. Why not use subl folder_name/ instead?

Either way my ST3 (build 3083) installation on OS X is opening a child directory with either subl ./child or subl child/ whether Sublime was open prior to the command or not.

PS: Make sure you don't have the sidebar closed when opening directories by running Command + K then B. I've often assumed a directory was failing to open just because I had my sidebar closed and couldn't see the files listed inside of it.

There is probably an alias with the name subl provided by 'Oh my fish' . You can check if there is an alias by using alias command in the terminal. This will display all aliases for your session. If you have it on the list then it is colliding with your symbolic link. Disable the alias by fixing the source or by using unalias subl (unalias will only fix it for the current session)

I had this problem when using bash-it aliases for osx. Disabling it fixed the problem for me.

For Linux and MacOs users and Sublime Text 3 Try the command : subl3

MAC OSX Open terminal and run following command:

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

The above command is needed only for first time. After this configuration, whenever you go to folder where your project is present, run following command:

subl .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!