A Real World VimGolf Challenge

In my CUA Mode Video post, I mentioned that the pointer to the video came from a comment to one of the posts in the Google+ Emacs Community. That post asks for advice on the best way of changing

a[0] = b[0];
a[1] = b[1];
a[2] = b[2];

to

c[0] = d[0];
c[1] = d[1];
c[2] = d[2];

My first thought (and what I’d probably just do if I needed this change) is a query-replace to replace a with c, followed by 【Meta+<】 to return to the top of the buffer and then another query-replace to replace b with d. That’s 13 keystrokes. Not too bad but can we do better?

Here’s a solution in 12 keystrokes using cua-selection-mode.

Ctrl+Return Invoke rectangle mode
Ctrl+2 Ctrl+n Form rectangle of a’s
Meta+f c Fill rectangle with c’s
Ctrl+7 Ctrl+f Move to last b
Ctrl+Return Invoke rectangle mode
Ctrl+2 Ctrl+P Form rectangles of b’s
Meta+f d Fill rectangle with d’s

Using Magnar Sveen’s essential multiple-cursors package I can do it in 9 (8 if I don’t exit multiple-cursors mode) with essentially the same strategy as above but, of course, that’s not using stock Emacs. If you have a clever solution, leave a comment.

This entry was posted in General and tagged . Bookmark the permalink.