/dev/nikc/blog

Kuolleiden purjehduskenkien seura

Shorts

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.

1 There is one shortcoming; only piped content works, not redirected input. If you can fix this, you're welcome to leave a comment, but I'm not losing any sleep because of this.

Meta

Pages

Search blog

Latest comments