Vim vs poor typography

Saturday, 25 Dec 2004

I frequently find myself fixing quotes and em-dashes in stuff in want to quote (changing "something" to “something” and this -- that to this – that) with a few substitutions in Vim. Recently, I got tired of recalling the relevant commands from history and tweaking them every time over, so I took what I thought would be a few minutes to write some custom commands for my .vimrc. The idea was to take the time to compose somewhat smarter patterns, hopefully solving this task once and for all.

It turned out to take a great deal longer than a few minutes for a reason I didn’t anticipate.

Vim seems to misunderstand non-ASCII characters in .vimrc no matter what I do. I tried :scriptencoding, read all the relevant manual sections twice, tried a number of tricks, all to no avail. Eventually, I got around the problem:

command! -range Fixquotes <line1>,<line2>s!\(^\|\s\)\zs"\([^"]*\)"!\="\u201c".submatch(2)."\u201d"!gc
command! -range Fixdash   <line1>,<line2>s!\(^\|\s\)\zs--\?\ze\($\|\s\)!\="\u2014"!g

It took altogether too much gnashing of teeth to get there.