Calling Sublime from Terminal

Terminal-SublimeI work a lot from the command-line. From SSH sessions to automation scripts. My finger memory is always wanting to “$ vim <filename>” when I need to edit a text file. There is certainly nothing wrong with VIM, but, Sublime Text Editor has far more useful keyboard shortcuts (including their famous multi-cursors).

So, I needed a way to call up the editor for a file from a command line on my Mac.  It’s pretty easy using the “alias” and “open” commands.  To open an application from the command-line, you would use:

NOTE:  See a simpler, alternative way at end of post

$ open -a Sublime\ Text\ 2 <filename>

Okay, easy enough.  But, I really do not want to type all of that in.  What a pain.  Let’s simplify it using the command ‘alias’ so I can just use the command ‘sublime’ to open:

$ alias sublime='open -a Sublime\ Text\ 2'

Now, if I run the command ‘sublime <filename>’ it opens my file in Sublime.  Awesome!  But, now, you’ll notice that if you close Terminal, it’s no longer available.  To make it persistent, you’ll need to add the command to your .bash_profile:

$ vim ~/.bash_profile

And, below, you’ll see my aliases in my profile.  This file gets executed each time your terminal (bash shell) gets started.  Make the appropriate edits.  Then exit your Terminal and re-open.  Viola!  Quick command-line editing using Sublime.

#Aliases
alias pluralsight='cd ~/Documents/Training/Pluralsight'
alias myteneo='ssh root@myteneo.net'
alias sublime='open -a Sublime\ Text\ 2'
alias dev='cd ~/dev/git/'

export CLICOLOR=1
export TERM=xterm-256color
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

UPDATE 2/23/2015:

There is a simpler way, with the idea from Yandy Ramirez (@IPyandy).  It’s funny, that sometimes, we seem to think in terms of complexity and advanced concepts, when really, the simple explanation stares us in the face.

The idea is to create a soft link in your system path.  Pick a directory in your system path by doing an “echo $PATH”

aaron.paxson@USNASLTF10624ab testing-vagrant $ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin

I chose /usr/local/bin.  Change into that directory, and symlink to the Sublime executable:

$ sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl

Now, you can use the command “subl” to startup Sublime.

 

Share This Page : Share on TwitterShare on FacebookShare on GooglePlusShare on PinterestShare on Linkedin