Sublime Text 2 Whitespace Plugin

I’ve recently ditched Textmate in favor of Sublime Text 2. I’ve also recently shelved Ruby for Python, and it’s pretty cool to be able to whip together some python to extend my editor. There are some tutorials for creating plugins and decent documentation, but sometimes code is worth a thousand words.

I found a nice set of plugins on github from ehamiter to get started with. It’s pretty easy to get the swing of things, just be prepared for a few infinite loops and force closing your editor!

Here’s the whitespace plugin I came up with to handle three issues vital to my day-to-day development: 1) removing trailing whitespace, 2) expanding tabs, and 3) adding a single newline at the end of the file.

There is an out-of-the-box Sublime Text 2 command for expanding tabs, which I wanted to run on every save, so the ExpandTabs class is a simple pre-save handler that calls the command. All three pre-save handlers check for a configuration settings (set via JSON preferences file), so if you don’t use one it will still be light-weight.

This works for me, but a slightly cleaner model would be to create TextCommand actions, in addition to EventListeners that call the command, so you get the best of both worlds (call commands with key bindings, or enable event listeners).

To install, I simply clone to the repo into my packages directory, i.e. git clone git@github.com:nstielau/Sublime-Text-2-Plugins.git /Users/nstielau/Library/Application Support/Sublime\ Text\ 2/Packages/nstielau, and then can easily sync the plugins across workstations via github.

One thought on “Sublime Text 2 Whitespace Plugin

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>