When I wrote The Case for Emacs, the main point I was attempting to convey was that Emacs is an amazingly effective editor even without the customizations it's so famous for. Right out of the box, you can do some pretty incredible things with its broad set of built-in commands. And if a simple Emacs configuration is good enough for Donald Knuth, it's good enough for us, right?

The other day I came across a CSS file that was in need of some formatting tweaks, so I slapped together a quick macro and fixed it up. Nothing special, but it got me thinking -- why not take this example to an extreme, creating the ugliest CSS file of all time, and create a screencast of cleaning the file up using a bare bones Emacs configuration? Well, here we are.

The CSS file you see getting the Emacs treatment in the screencast is not real. I intentionally created about the ugliest file I could, butchering indentation, casing, structure, mixing tabs and spaces ... you name it (the CSS file is attached, if you're especially curious).

I'm not necessarily suggesting that Emacs is the best tool for the specific task we're performing here, but it's a pretty broadly understood file format, so I thought it would be interesting way to demonstrate some of Emacs core functionality. If your favorite CSS editor has a magical "auto-format" button, by all means use it -- it's better than this strategy to be sure, but it's a lot less flexible!

The Emacs instance you see in the screencast is extremely close to a stock distribution. I made the following modifications, in the interest of making it easier to see what's going on:

Disabled the tool bar and menu bar. Loaded mwe-log-commands.el, for demonstrating the keys pressed. Enabled the downcase-region command, normally disabled. Loaded a simple CSS mode for font locking. Changed the default font to a narrower version, Consolas.

On with the show.

Here's a breakdown of how we attacked the file.

Converted tabs to spaces with untabify . This is a good first step when encountering a file as hideous as this one. Normalized all spacing by compressing multiple spaces and newlines to a single space, using a regular expression replacement. Added newlines after all open braces ( { ) using a macro. Also used just-one-space before the brace to make the spacing consistent. Added newlines after all semi-colons, again using a macro. Also compressed space in front of the semi-colons, and added a space after the colon delimiting the property from the value. Used another macro for adding newlines before and after the close braces. I added the extra spaces before using delete-blank-lines because the spacing varied based on whether or not a trailing semi-colon was present. Killed a few empty blocks, using backward-paragraph to quickly navigate blocks. Executed another macro to make the property name case consistent (made them all lower-case). Sorted properties by name within each block using a macro to regionize the block and call sort-lines . Compressed expanded forms of margin and padding specification to a single line, using a multi-line regular expression replacement with a reference to a captured group in the replacement string. Performed a few simple manual cleanups, and updated the messaging.

Attachments