Let's Play Ivmgolf

After my last VimGolf post I thought it would be fun to try another so I headed over to the VimGolf site to find a challenge. The first one I stumbled across was Let's play some Imvgolf. The challenge is simple: starting with

Ivm is an awesome text editor based on
Iv, and is used to play a game called
Ivmgolf. A challenge, simple for many
Ivmgolfers, can still hide secrets.

transform it to

Vim is an awesome text editor based on
Vi, and is used to play a game called
Vimgolf. A challenge, simple for many
Vimgolfers, can still hide secrets.

in the smallest number of keystrokes possible.

My first thought was that it was too simple. Surely, it would hard to beat a simple query-replace: 【Meta+%ivReturnviReturn】 【!】 for a total of 8 keystrokes. Hard to see how you could do much better than that. Except that the winning VimGolf entries solved it in only 5 keystrokes1

Using Magnor Sveen's awesome multiple-cursors package, which should and probably will be part of Emacs core, I was able to get that down to 6 keystrokes.

It's easy to see what you need to do: switch the first two characters of each line and adjust the capitalization. I tried that using keyboard macros and even apply-macro-to-region-lines but I couldn't do better than query-replace using core Emacs functionality. The problem is that any macro that requires typing in Vi has already used 4 keystrokes, and if you use a macro and something like 【Ctrl+t】 to switch characters you've used 3 keystrokes and still have to adjust the capitalization, get to the start of the next line, and run the macro on the rest of the lines. If you can do better than 8 keystrokes with stock Emacs, be sure to leave a comment.

Footnotes:

1 The VimGolf rules say you have to save the file too so that adds an extra 2 keystrokes in both Vim and Emacs but I'm ignoring them since they add nothing useful to the challenge. All scores in this post are without saving the file.

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

5 Responses to Let's Play Ivmgolf

  1. xah Lee says:

    am thinking replace-string, which can save 1 key than query-replace. But replace-string doesn't have a key, so doesn't really apply. Interesting question. How the vi guys do it in 5?

  2. Magnar Sveen says:

    I managed five keystrokes using my keybindings with expand-region, multiple-cursors and delete-selection-mode. Not exactly stock emacs tho.

    - C-n (down to the Iv)
    - C-@ (expand region to mark Iv)
    - M-æ (mark all like this)
    - V
    - i

  3. Brad Wright says:

    I got 7 with iedit - not great:

    C-n
    C-space
    M-f
    C-; (iedit)
    C-b
    C-t (transpose-letters)
    M-c (capitalize-word)

    • jcs jcs says:

      Like Magnar and me, you're cheating by using iedit because it's not part of Emacs core. We did essentially the same thing using Sveen's multiple-cursors, which is similar to iedit.

      You can save a keystroke by using Shift+Meta+f instead of the two sequences Ctrl+Space and Meta+f.

Comments are closed.