Another use for Google Docs

October 20, 2006

I have found Google Docs to be very useful for maintaining a private cheat sheet of some of my favorite, but less memorable vim commands.

How great is vim’s regex replacement.

I am trying to convert this html site over to php. The html page has http://www.example.com/locations/ak.html and i need to change all 50 of those links over to http://www.example.com/locations/location.php?abbr=ak. With vim, it took less than a minute and saved my fingers from fatigue (although, this post just voided that).

Here’s how it’s done:

:%s/href="locations\/\([a-z]\{2}\)\.html"/href="locations\/location.php?abbr=\1"/gc

Use \(load-this-text-match-to-memory\) in the search and \1 in the replace to load the first value in memory.

Often I want to to match a pattern, but only replace a segment of that pattern. Here’s how: :%s/foo\zs9\ze/bar/gc will make foo9 => foobar. This may be useful when needing to replace with some incremental variable.

To learn more, :he sub-replace-special