camdez.com

Rule #1: There are no rules.

Whiteout - Say 'No' to Whitespace

| Comments

I recently whipped up a command-line utility / Ruby gem called ‘whiteout’ which removes trailing whitespace from files. As you would imagine, it’s a simple piece of code, but it’s a bit more convenient than the shell script I was using. It also gave me a chance to learn how to cut a gem and how to structure a command-line utility in Ruby.

You can use whiteout to remove trailing whitespace from a single file, a directory of files (recursively), or from standard input:

1
2
3
$ whiteout foo.rb
$ whiteout -r bar/
$ echo -e "quux   \nxyzzy" | whiteout - > baz.txt

Convenient, right? And you can install it with a simple gem install whiteout (as 230 others already have!).

One could make a perfectly reasonable case that Ruby is too slow of a language for this type of utility, but I tend to think that in this age of spare cycles the most important aspect of a utility is defining its interface and establishing its presence. The underlying code can be swapped out later.

git whiteout

Let’s call a spade a spade: checking in code with janky whitespace is shameful. But fighting Xcode’s whitespace habits is time-consuming. Instead of bothering, drop this in your .gitconfig:

1
2
[alias]
  whiteout = !whiteout `git ls-files -m`

Now, when you run your pre-commit diff and find messy whitespace, just run git whiteout to remove trailing whitespace from every file you’ve modified. Go, go, programmer laziness.

Comments