A VimGolf in Emacs Challenge

I've been obsessing and writing about the continuing NSA scandal and as a result we haven't had any fun with VimGolf for a while. Just in case you're as rusty as I am, here's an easy one.

Starting with

one two

transform it to

(one) (two)

in the minimum number of keystrokes.

It's easy to do this with a straightforward query-replace-regexp in 14 keystrokes, which is about the average for the VimGolfers. A slightly more clever use of query-replace-regexp gets the job done in 10, which is better than all but one of the VimGolf solutions.

Those are fairly pedestrian solutions but we can do better by leveraging some Emacs power. Using a macro in conjunction with paredit does it in 6. Unfortunately, the best VimGolf solution takes only 5. So it's up to our Irreal experts to save the honor of Emacsers everywhere. Add your solutions to the comments and I will post mine in a few days.

Update: I forgot the give a link to the original VimGolf problem. Here it is. Also, I notice that the solution that solved it in 5 (7 according to VimGolf rules) has been removed. I don't know what that means.

Vimgolf → VimGolf

This entry was posted in General and tagged . Bookmark the permalink.
  • More straightforward solution for stock Emacs, as for me, would be using macros, which takes 8 keystrokes.

    • Phil

      Likewise, except 7 keystrokes is sufficient:

      f3 ( M-C-f ) C-f C-0 f4

  • Francesco

    Here is what I would do:

    f3 -- begin macro

    C-x SPC -- mark word (using Magnar Sveen's expand-region)

    ( -- surround word with parentheses (using autopair)

    M-2 C-right -- go to next word

    M-3 f4 -- end macro and run it twice

  • You've already posted this one.

    F3, sp-select-next-thing, (, C-0 F4

  • Anonymous

    What was the 5 step vim solution? For that matter what is the 12 step vim solution that currently is the best?

    • jcs

      The VimGolf site doesn't provide the solutions of the top scorers until you provide your own solution so I don't know what they were.

      • Phil

        I think some people are playing their own game on the VimGolf site -- bottom of the list is a 92 keystroke solution :)

  • a custom function can do in 5. Press a button to bracket first word. Move cursor down, press button again, then cursor up, again. So, 3 keys to bracket each word, 2 keys to move cursor down and up. I do this from my emacs setup. The function is insert-pair-paren, which is defined as
    (defun insert-pair-paren () (interactive) (insert-bracket-pair "(" ")") )

    the insert-bracket-pair you blogged about. I made some modification later that wrap the paren if cursor is around word.

  • Daniel


    there is a stock emacs function to insert parantheses even around 'n' words: `insert-parantheses', when called with a prefix arg it will wrap that many words in parantheses. For the given problem I would do:

    F3 M-1 M-( M-Right F4 M-2 F4 (7 keystrokes)

    I have M-Right bound to forward-to-word from misc.el instead of forward-word as it is by default (I can still access forward-word via C-right).


    • Phil

      F4 M-2 F4 reduces to M-3 F4

  • Jiehong

    With normal emacs, no need of auto-paren:

    M-@ — select word
    M-( — add parentheses around selection
    C-M right — move to the end of the word
    right — move to the beginning of the next word

    8 is pretty long…

    • Phil

      I like this approach. I never use M-@ or M-( but this makes me think that I need to remember them. Note that you can reduce your three F4s to M-3 F4 (or zero to repeat as many times as possible).

      • jcs

        Me too. I always have a hard time remembering those bindings but they are often just what you need.