A Simple EmacsGolf Challenge

We haven’t had an EmacsGolf challenge for a while so here’s something simple that was suggested by Xah Lee based on a Stack Overflow question. The Stack Overflow questioner wanted a solution in Vim so the whole thing fits right in with our never ending quest to solve problems more efficiently in Emacs. The best answer at Stack Overflow using Vim was 53 keystrokes. Let’s see if we can beat that.

The starting buffer is 10 lines each beginning with a time in minutes:seconds as shown.

05:16 more line 1 text
06:32 more line 2 text
13:50 more line 3 text
27:36 more line 4 text
33:07 more line 5 text
33:12 more line 6 text
41:03 more line 7 text
48:22 more line 8 text
51:05 more line 9 text
57:58 more line 10 text

We want to change the time into seconds

316 more line 1 text
392 more line 2 text
830 more line 3 text
1656 more line 4 text
1987 more line 5 text
1992 more line 6 text
2463 more line 7 text
2902 more line 8 text
3065 more line 9 text
3478 more line 10 text

There are a couple of ways of doing this. One uses query-replace-regexp and works with stock Emacs. The other uses Magnar Sveen’s terrific multiple cursors in a way similar to his last example in the multiple cursors video. Using multiple cursors takes me 22 keystrokes. Can you do better?

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

6 Responses to A Simple EmacsGolf Challenge

  1. Phil says:

    An initial attempt with stock Emacs is 36 keystrokes:

    C-x h M-% : * 6 0
    + ! C-x h C-M-% ^ C-8 . \ , ( c a
    l c - e v a l SPC \ & ) !

    With multiple cursors (mc/edit-lines on s-SPC s-SPC) and eval-and-replace (on C-c x e) I get 20 keystrokes:

    C-x h s-SPC s-SPC ( + ( * SPC M-f C-d SPC 6 0 ) M-f ) C-c x e

  2. Francesco says:

    I have a solution similar to Phil’s using multiple-cursor (with slightly
    differing key bindings).

    Using stock emacs and a macro takes me 24 key strokes (easily reduced to
    around 21 with a binding for eval-and-replace):

    <f3> ( + ( * SPC 6 0 SPC <C-right> ) <delete> <C-right> ) C-u C-x C-e <C-left> C-SPC C-a C-w <down> M-0 <f4>

  3. Tuomas says:

    I really don’t get why someone would count e.g. C-M-% as one key for this purpose.

  4. Alex says:

    With custom emacs a standard solution using Query replace regexp could be the following (it is not the shortest, but still considarably shorter than the Vim solution):

    M-%
    ^\(..\):\(..\)
    \,(+ \#2 (* 60 \#1)) !

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>