A Solution to the VimGolf in Emacs Riff

Recall that the last VimGolf in Emacs challenge was to convert

abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

to

a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z

I found this harder than I expected. It's pretty easy to put a space after each character but then you have to deal with the spaces after the z in each line. The previous challenge was easier because all we had to do was find the blanks and replace each of them with four blanks. In this case we want to insert a blank after each character unless it' the last character in the line.

Here's my solution:

Ctrl+Meta+% Invoke query-replace-regexp
. \ B Return Match every character except the last in the line
\ & Space Return Replace with the match followed by a space
! Run for the entire buffer

That's a total of 10 keystrokes. Not too bad, but Aaron Culich managed to shave off 3 (!) more keystrokes by realizing that you don't have to match the letters, just the empty spaces between them. Thus the regexp for the FROM string is just \B, which means that the TO string is just SPACE for a total of 7 keystrokes. If you can do better, be sure to leave a comment. If you're a Vim expert, how many keystrokes does it take you?

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

    I certainly can\'t better 7 keystrokes, but for a fun alternative with cua-selection-mode I can do it in 10 keystrokes using rectangle editing to insert a space down the entire column, and a macro to repeat it for the rest of the line. That\'s also more fun when you undo it :) (the same thing with standard rectangle commands takes me 14 keystrokes).

    • http://irreal.org jcs

      Yup, it's hard to see how you can beat 7. In the same vein as your solution, I can do it in 9 using Magnar Sveen's multiple-cursors, which is probably what I would have done in "real life." Actually, this is one of the few problems where multiple-cursors doesn't enable the best solution.