Validate on Save for TextMate

I've been using TextMate almost exclusively for years for writing all kinds of code, shell scripts, PHP, Ruby, JavaScript, HTML, CSS... it's one of those programs that excels at the thing that it does, and the thing that it does is get out of the way and let you do your thing.

I never seemed to outgrow it. Nearly everytime I've wanted it to do something, either someone else put something together or I've been able to extend it by writing my own commands or modifying an existing one.

One of the things I've done is bind the shortcut to save (cmd+s) to also validate my PHP or Ruby. Total time saver. If you're a web developer you're probably well aware of the save/switch/refresh/crash/switch/fix/save dance that one does between code editor and browser.

Recently, I decided to package it all up and throw in a couple other validators into it, and thusly the Validate on Save TextMate Bundle was born. This bundle will bind the save shortcut for you, and validate your PHP, Ruby, JavaScript, and CSS.

If you're running Compass (a framework to Sass's CSS abstraction)1 this tool will try to compile your Sass files and return any errors it outputs.

If you install with Git using the instructions in the README, you'll have instant access to updates, just click the Bundles > Validate On Save > Update Bundle menu command.

There are a few other configuration options in there, like using Growl for notification or jumping directly to the line the validator found errors, but in both cases, the results were a bit iffy so you'll have to enabled those (by following the helpful instructions).

I showed an early version of this to my friend Jim Myhrberg, who refused to let me get away with distributing my messy code, so he did a ton of reorganization and cleanup that ultimately make this even easier for us to perhaps someday add more validators.

  1. If you have no idea what I'm talking about but write CSS, do yourself a favor and check it out. Highly recommended. 

SVN with a bit of ADD

I tired of manually adding all the new files in my working copy of various projects in subversion so I started using this command:

svn add svn status | grep ? | awk '{print $2}'

... but I eventually grew tired of looking that up every time. I tried to make that into an alias, but because of the shell escaping it didn't work as is. I did a bit of digging around and found a non-shell escaped version then added these lines to the bottom of my ~/.profile (I'm sure it would also work in ~/.bash_profile):

alias svnadderall="svn status | grep '^?' | cut -b 8- | xargs svn add" alias svnridallin="svn status | grep '^!' | cut -b 8- | xargs svn remove --keep-local"

A quick breakdown of how this works:

  • alias svnadderall =
    this is the command that tells bash what will happen when you type svnadderall. You can change that to whatever you'd be more comfortable with. I used "adderall" and "ridallin" because I'm clever like that (I'll eventually get sick of the puns, but the ADHD mnemonic will help me get in the habit of using them (yes, I know they're not spelled correctly; they're phonetic mnemonics)).
  • svn status |
    prints out the status of all your files and passes it to
  • grep '^?' |
    finds each line beginning with ? and passes it along to
  • cut -b 8- |
    trims off the first eight characters and returns the rest of the lines and passes it to
  • xargs svn add
    captures everything and runs svn add [whatever it was passed]

It's the same for the svn remove except the grep is matching for lines that begin with ! and I'm using a --keep-local flag to prevent svn from trying to remove the file1 (which won't exist, since it's missing, remember?).

I tried changing the cut -b 8- to sed 's/^?[ ]+//' and even awk {print $2} but neither worked properly for some reason; the awk based version tried to svn add ? and the sed version sat there waiting for input.

On further consideration, I think cut -b 8- will be the safest. The file name won't always be the second item in the svn status output. I can't imagine any situation where that would be the case for non-versioned files, though there might be true for missing versioned files would have a secondary (or tertiary) flag.

If you're editing your bash_profile or profile be sure to run source ~/.profile (or source ~/.bash_profile) if you want your shell to pick up your changes. You can also logout and log back in (or open a new Terminal.app tab or window) as well.

  1. I don't know if it's an subversion v1.6 thing or not, but doing an svn remove on files that do not exist on the file system would exit on the first error, in this case being that the file doesn't exist. 

Bookmarks for February 19th

Copyright © 2004 - 2010 M. Auayan. All rights reserved.