Solving Problems With Better Tools, Or How Vim Macros Are Underrated

Vim’s creator, Bram Moolenaar, says his 2 favorite features of vim are macros and the “dot repeat” command.

I can appreciate why. No other editor seems to have macros that are as easy to use (Except Emacs, of course. It can even emulate vim’s macros).

I use NeoMutt as my mail client. I even gave them their tagline, “Teaching an old dog new tricks”.

Today, I decided to see if I could contribute to the project with code rather than prose, despite not knowing C.

One task that needed to be done was internationalizing strings. This amounts to essentially wrapping each string in parentheses and prefixing them with an underscore and another set of parentheses.

Thanks to a combination of features from 2 very handy tools, vim and ag, I was able to do this in about 15 minutes.

The first step was to normalize all strings that were already internationalized by wrapping them in parentheses. I used a regular expression to find them, opened them up in Vim, and used the quickfix list and a macro to modify them.

Vim has a feature called the quickfix list which is just a bunch of marked lines that you can jump between, usually for compiler errors. However, tools like ag and grep can print their search results in a way that’s compatible with a quickfix list, and you can jump between your search results and change each one. Since it will jump you to the exact line and column where each match starts, it’s easy to operate on each one.

The next step was to wrap the remaining strings as described above. I used a regular expression to find them, ag’s ability to print out the results in a way that vim’s quickfix list could consume, and another macro to actually do the wrapping.

I used a macro that essentially jumped to the opening quote of the string in question, wrapped it, and then jumped to the next string. Then it was just a matter of typing 1376@@ (that’s how many matches there were).

And that’s how you can make a pull-request by just using powerful but simple tools together.

Related Posts

How to Disable Disqus Ads on your Blog

Derivation of Reservoir Sampling

Fun with Python Iterators: Linked Lists Made Easy

Notes for November 11, 2018

Underrated Vim Option: undofile and undodir

Hot Take on Solo Travel: Starve

Alan Perlis

Book Notes: The Map of My Life by Goro Shimura


Way to remember the definition of local finiteness