/dev/nikc/blog

Kuolleiden purjehduskenkien seura

Jan 4th 2012

Sublime Text 2 and the command line

15:15

Since about half a year ago I migrated from Textmate to Sublime Text 2, simply because it’s awesome. Of course, as with all software, it has its shortcomings, but nothing I can’t live with.

But one thing which really bothered me and occasionally made me return to Textmate was Sublime’s lack of a command line companion tool. I spend a lot (most) of my time on the command line and the lack of a command line helper was a real drag. But as I like both the command line and Sublime Text 2, and not willing to give up either, I simply wouldn’t believe the situation couldn’t be remedied and embarked on a mission to find a solution.

If all you need is a shortcut for opening files and folders, a simple alias is enough:

alias slt=open -a "Sublime Text 2"

But this doesn’t allow for creating new files or the use of pipes. Both of which are frequent use cases for me. To accomplish this, a bit more code in the form of a bash function comes to the resque:

function slt() {
    if [ -z "$1" -a -t 0 ]; then
        echo "slt file [...]"
        return 0
    fi

    if [ -z "$1" ]; then
        open -a "Sublime Text 2" -f
    else
        for arg in $*; do
            [ ! -f "$arg" -a ! -d "$arg" ] && touch $arg
        done
        open -a "Sublime Text 2" $*
    fi

    return 0
}

And voila! Copy and paste into your .bashrc and your new best friend can now be used to edit new files and read stdin1 from the command line.

Disclaimer: This script hasn’t been tested on anything other than OS X 10.6.8. Most likely if you’re on Linux you won’t need any of this extra malarkey, and if you’re on Windows, well, you have my sympathies.

4 Responses to “Sublime Text 2 and the command line”

I am not a coder but i do declare that Sublime Text 2 looks like an editor i would like to use when i tinker with my scripts and stuff.

Thanks for they sympathies. Later, i’ll check out ST at home on a Linux box.

~rL

Have you tried the command line helper, which somes with Sublime Text 2?

http://www.sublimetext.com/docs/2/osx_command_line.html

s/somes/comes/

I swear that wasn’t in there when I started using ST2. Me feel silly now.

Sorry, the comment form is closed at this time.

Meta

Pages

Search blog

Latest comments