How to fix opening sublime from terminal in MacOS? - macos

I want to be able to open the sublime text editor in MacOS (14.10.6) from the command line. I found several instructions how to do that (HERE and HERE), but of course it does not work for me.
I did create the symbolic link to the application in /usr/local/bin:
sublime -> /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl
The file at location /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl does exist. Also, the folder /usr/local/bin is included in the actual definition of PATH.
However, the command is not found when I enter it on the command line in a terminal
~$ sublime
-bash: sublime: command not found
Proof:
$ls -al /usr/local/bin | grep sublime
lrwxr-xr-x 1 root admin 63 Jan 15 07:44 sublime -> /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl
$ echo $PATH
/usr/local/opt/python#3.8/bin:/usr/local/opt/python#3.8/bin:/Users/adietz/miniconda3/bin:/usr/local/bin:/Users/adietz/.pyenv/shims:/Users/adietz/opt/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/puppetlabs/bin:/opt/X11/bin
Any ideas how to fix that?

You may use the open command, as the manual says:
DESCRIPTION
The open command opens a file (or a directory or URL), just as if you had
double-clicked the file's icon. If no application name is specified, the
default application as determined via LaunchServices is used to open the
specified files.
Option -a is suitable for MacOS applications, then
open -a sublime\ text
(or alike) should work.
You can then alias it and call it when needed:
alias sublime="open -a sublime\ text"
sublime

Related

Difference between symlink and alias

I was trying to set up opening sublime text editor from terminal on Mac OS.
When I was searching for that I found out one way to do that: create soft link (symlink) and change the path in .profile by ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime but since PATH did not change in .profile I just added alias sublime=' open /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl'
Could you please tell me what is the difference between these ways?Which way is better?How to change PATH from terminal not opening .profile?

Sublime Text 3 'subl' command still linked to Sublime Text 2

This is driving me crazy. I'm running OSX and until today I had Sublime Text 2 installed. After installing Sublime Text 3, when I enter the 'subl' command the terminal logs:
Unable to find application named 'Sublime Text 2'
I've tried everything. I've tried
$ rm ~/bin/subl
Then
$ ln -s /Applications/Sublime\ Text\ 3.app/Contents/SharedSupport/bin/subl ~/bin/subl
I've also tried going in to usr directly and removing the subl command. When I do this, the subl command will sometimes work while I remain in one directory, but once I switch directories it goes back to giving me the message
Unable to find application named 'Sublime Text 2'
With Sublime Text 2 I had subl entered as an alias. I also tried going in and unaliasing subl like this:
unalias subl
And I've tried re-creating the alias using
/Applications/Sublime\ Text\ 3.app/Contents/SharedSupport/bin/subl
Again, this will work for a while, but once I change directories it stops working.
Any advice on what to try next? I'm at a loss. I can't understand why it works for a while, then stops working.
Check your .bashrc file, and .bash_profile and see if it's aliased in there. Both of these are in your home directory ~.
nano ~/.bashrc
nano ~/.bash_profile
No need to sudo as you own these documents.

Why do I get "subl: command not found"?

I am trying to access and manipulate the .gitignore file in Sublime via the terminal and I ran into this:
subl .gitignore
-bash: subl: command not found
Why can't it find it?
From the official documentation:
The first task is to make a symlink to subl. Assuming you've placed Sublime Text 2 in the Applications folder, and that you have a ~/bin directory in your path, you can run:
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

command "subl" from terminal don't work

I am a Mac-newbie (Mountain Lion) and try to set up the "subl"-command for the terminal, like described here:
The first task is to make a symlink to subl. Assuming you've placed
Sublime Text 2 in the Applications folder, and that you have a ~/bin
directory in your path, you can run:
ln -s "/Applications/Sublime Text
2.app/Contents/SharedSupport/bin/subl" ~/bin/subl
But it doesn't work. I have now a folder /bin/ under my user and this folder including a alias. But when I tip in subl --help inside the terminal, I get the error -bash: subl: command not found
Can someone helps me out?
KR & thx, Fabian
Change directories to:
/usr/local/bin
then run this instead:
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" subl
I have found the solution now - there was a problem with RVM. The solution is to enter the command
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/.rvm/bin/subl
into the terminal.
That happened to me with Sublime Text 3.
This symlink works for me, only I needed was to change the symlink destination to /usr/local/bin/subl
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Solution source Paul Byrne answer here
Using Homebrew to install Sublime usually creates the correct symlinks automatically.
brew install Caskroom/cask/sublime-text
Subl can then be accessed like:
# Open Sublime at current directory
subl .
The solution chosen for this question is to put the subl symlink in the ~/.rvm/bin folder which is definitely not a generic solution as this directory is part of a program he has installed.
Although any directory in the path will work, the most common solution is to create a bin directory in the home folder, set it in the path by putting export PATH=$PATH:~/bin in .bash_profile and then running the link command as shown in the original question with this new path.
In the past it was common to put the subl symlink in /usr/local/bin but this directory no longer exists in OSX as of version 10.2 and the /usr/local directory is no longer in the default path.
SOLVED for MAC OSX Mavericks!
I found this answer on this GitHubGist:
https://gist.github.com/artero/1236170
Launch Sublime Text 2 from the Mac OS X Terminal
ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime
This is the part that I found clarified what many StackOverflow responses have only hinted at, but not explicitly explained:
Now let's do a check to see if everything will run smoothly. Enter this:
open ~/.bash_profile
(In some cases the profile file is named ~/.profile)
You should see at the top of the file a line that starts with: export PATH=
This contains all the directories that will be looked into for executable binaries when you type a command in Terminal. Since we create a symlink to subl called sublime in the /usr/local/bin directory let's check if this directory is listed on that same line.
If it is, perfect. Let's keep going. If not, simply add it like this and save the file:
export PATH=/usr/local/bin:(...)
Note: The '(...)' in this example represents other folders that would be listed on the same line and separated by a colon.
If you don't already have a PATH set in your bash_profile you can type:
export PATH=/usr/local/bin:$PATH
If you had to add /usr/local/bin to your PATH, run the following command before continuing:
source ~/.bash_profile
This will reload your .bash_profile with the newly added directory.
Testing
Open a Terminal window and run:
sublime filename (replace "filename" by an actual file name)
Hope that helps clarify for others as well. Currently (FALL 2014) using Mac OSX Mavericks. Cheers
If you just want the command to open files using sublime text, and you're not bothered to use it as your default editor for all commands, why not just add an alias to your ~/.bash-profile ?
alias subl="open -a 'Sublime Text'"
That will work for opening files and directories
Had the same problem on a MAC with OSX Yosemite 10.10.5, and sublime text 3 Build 3083!
Configuring git to work with the above requirements worked perfectly using:
git config --global core.editor "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n -w"
And test if it worked with:
git config -e
It works on Mac OSX 10.8.2, echo $PATH shows subl symlink correctly linked as described in this post. The other suggestions on Sublime Text 2 page seem not work with new Macs
I discovered that there was an alias for subl. Type alias to see all defined aliases. Find the source and comment it out, or use unalias subl to unset.
For .bashrc or .zshrc
alias subl="open . -a /Applications/Sublime\ Text.app"
Catalina's auto-sandboxing seems to broke the subl command.
Use the following alias as an alternative
alias subl="open $1 -b com.sublimetext.3"
After $ subl . did not work for me,
I tried $ bundle exec subl . and it worked.
Solution: type $ bundle exec subl . and it should work. (Obviously, omitting the $)

Installing Sublime Text's command line tool 'subl' in terminal, permission denied?

I'm tryng to use Sublime Text from the terminal, for example by typing subl.
I'm following the steps from Sublime Text's website:
Setup
The first task is to make a symlink to subl. Assuming you've placed Sublime Text 2 in the Applications folder, and that you have a ~/bin directory in your path, you can run:
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl
I keep getting permission denied : ~/bin/subl: Permission Denied
I have been searching around for a similar problem but can't find one that's applicable. Can someone point me to the right direction?
I am assuming that you don't have the bin directory. You can do the following:
cd
mkdir bin
sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl
ln -s will create a alias of the subl file in your bin directory and you will be able to run the command.
If this still doesn't work you will have to edit your .bash_profile. You can do it by following commands: (NOTE: For this to work you need to have done the above steps already.)
Open your .bash_profile:
cd // this will get you back to home directory
vim .bash_profile // this will open your .bash_profile file
Edit .bash_profile: press I to get into "insert" mode and add following:
export PATH=$PATH:~/bin
Save and exit. Press Esc to get into command mode:
:wq // saves and close file
exit // exits terminal
Reopen the terminal:
subl --help
That should bring up the help for Sublime Text.
My personal preference for the path to the third-party application-specific symbolic links (e.g. subl, brew, github, mate, etc) is:
/usr/local/bin
Why not /usr/bin/?
/usr/bin is a "sacred" place. It is generally recommended to store static binaries that are maintained by package management systems. subl is not this case.
subl is not stable enough to be stored in /usr/bin with other basic BSD binaries (e.g. find, man, make, etc). You must modify/delete subl symbolic link manually if (a) the developers of Sublime Text Editor decide to change its app name in the future releases (as BBEdit Lite was changed to TextWrangler after version 6.1), or (b) you may simply wish to uninstall Sublime Text Editor.
Therefore, I suggest you execute the following line, assuming /usr/local/bin/ exists:
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Check whether "~/bin/" is included in the path.
A better options is to create the symlink in /usr/bin directory instead.
sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/bin/subl

Resources