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.
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 email@example.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.