An Easy VimGolf in Emacs Challenge

Here's an interesting VimGolf challenge. As they say in the challenge, it's a simple problem but they're looking for interesting solutions. The challenge is to turn this


into this


The obvious solution that any Emacs user would think of does the job in 81 keystrokes.

Ctrl+s = cursor to =
Ctrl+k kill after =
Ctrl+n down one line
Ctrl+y insert ␠SOLUTION_FOLDER
Ctrl+k kill rest of line
Ctrl+p up one line

The best Vim solutions also do it in 8.

What I really want to do is something along the lines of

Ctrl+s = Ctrl+s Mark rectangle
Ctrl+x r k Kill rectangle
Ctrl+x Ctrl+t Swap lines
Ctrl+< Beginning of buffer
Ctrl+x r y Yank rectangle

that's 12 keystrokes, 50% more than the obvious solution. If I use CUA Selection Mode (as I wrote about previously) I can do it in 9 with essentially the same method as above. Is there a clever way of doing this or is the obvious solution with 8 keystrokes the best we can do? If you can beat 8 keystrokes—especially if it's something clever—be sure to leave a comment.

Update: Fuco does it in 6.

Update 2: Fuco shaves off another keystroke to a total of 5. Definitely a heavy weight contender.


1 Using our usual rules of changing the text but not saving the buffer.

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

    How about

    C-s =
    C-RET (enter cua)

    • jcs

      Wow! That's beautiful. I'm not sure if it's a configuration thing but your solution didn't work as is for me. I had to exit isearch with a Return before I could enter cua-selection-mode and then I had to use Meta+Shift+r instead of Ctrl+Shift+r to reverse the lines. That does it in 7 so it's a definite win.

      • Fuco

        Oh, sorry, It is indeed M-S-r, I was mindlessly copy-pasting the lines from above. As for exiting search-mode, I'm not aware of setting anything special, but I'll look into it.

        • jcs

          Oh, I know what it is. I mapped Ctrl+Return to exit the other end of the isearch string as I wrote about here. So your solution does it in 6. Most excellent!

      • Fuco

        (Ok, I'm sorry for spamming, if there's a way to edit the posts, or merge them, someone please do it)

        I rememberd what I changed just as I sent the comment :P I use this to get rid of useless terminal emulation, as I almost never use emacs in terminal, and when I do, I can just disable this anyway. I don't actually understand how the remapping work *in detail*, but this does the job done. If there's a better way, I don't know.

        You can then bind stuff to C-m by using (kbd "C-"), similar for the other two.

        ;;; stupid terminal key sequence remapping
        (define-key key-translation-map [return] [?\r])
        (define-key key-translation-map [?\C-\m] [(control m-key)])
        (define-key function-key-map [return] nil)
        (define-key function-key-map [?\r] nil)

        (define-key key-translation-map [tab] [?\t])
        (define-key key-translation-map [?\C-\i] [(control i-key)])
        (define-key function-key-map [tab] nil)
        (define-key function-key-map [?\t] nil)

        (define-key key-translation-map [escape] [?\e])
        (define-key input-decode-map [?\C-$$!] [(control left_bracket)])
        (define-key function-key-map [escape] nil)
        (define-key function-key-map [?\e] nil)

  • Fuco

    Here's a better still solution:

    C-s =
    M-e (end-of-sentence)

    I think this is about it, without using custom functions.