VimGolf in Emacs

It's been a while since we Emacs enthusiasts have locked horns with the users of that other editor. Here's a simple VimGolf challenge to warm us up. Starting with the buffer

one two
three

end up with

(one) (two)
(three)

The best Vim score was 12 keystrokes. I did it in 7 without trying very hard; I just did the first thing that came into my head. So the challenge for Irreal readers is to do better than 7. I have no doubt that one of you will do significantly better.

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

22 Responses to VimGolf in Emacs

  1. Fuco says:

    I have got it in five :P

  2. Toby Haynes says:

    Do we have to stick with the standard functionality? If not, I can do it in three (five if you count Ctrl and Shift). Elisp available on request.

    • jcs jcs says:

      My solution uses paredit so I guess any of the "standard" packages is OK. I'll post again with my solution and give everyone a chance for fame and glory then.

  3. mhd says:

    Well, the vimmers do it with a pretty bog standard init file. I mean, both editors can bind arbitrary scripts to custom keys, so we should probably meet them at their level. That would probably mean fundamental mode, too.

    Regarding "keystrokes", they seem to count e.g. "C-v" as one, but I doubt they'd agree that "C-x e" counts as one, too.

    Within those constraints, I can't even match 7...

    • Xah Lee says:

      that's true. it goes to show how arbitrary all this is. The vimgolf things doesn't measure real efficiency, power, nor take practical considerations. A alterative, might be to measure time instead, or, at least consider timing as 50% of the score, and the test pulication must be in real time (so that participants don't get to practice/experiment over it). And all command call should be counted as 1 keystroke... but thinking about this the whole thing is complex. The rules will be fairly complex, and depends on what exactly we want the score to represent.

      the naming also bites me. vi users tend to be in sync with unix culture, so we have perlgolf, etc. Emacs community tends to be in sync with lisp world, and don't much go with juvenile puns...

      • mhd says:

        seriousp

        Names alone: EINE, ZWEI, planner, conniver, scheme(r), SHRDLU and "Franz Lisp". Never mind the unending series of koans and haikus. Regarding sillyness, the two cultures really overlap a lot. Academia...

        And yes, this is pretty arbitrary. Both the rules and the problems themselves. Just looking at this example, just add a few words and the efficiency of solutions will change, favoring regexps or macros over pure repetition.

        I see three points for this: Competition with other emacs users, comparing conciseness with vim and beyond that, showing different approaches. The latter even when they're not as short, which is why even the vimgolf site shows you a plethora of solutions and users intentionally submit longer ones that the current front-runner.

        • Xah Lee says:

          there's a qualitative difference between puns used in the unix culture and emacs/lisp world. The ones you cited are not similar to unix ones. In some sense, they overlap because both are “hackers”, programers, but within this group, they are opposite camps.

          this can be easily seen from emacs docs vs unix man pages.

          were you oldbie lisper? e.g. working in 1980s or even early 1990s? i'm not, am just wondering. Because younger generation are oblivious to this. I have, however, read quite a bit of unix and lisp history.

    • jcs jcs says:

      When I first started posing the VimGolf in Emacs challenges after Tim Visher stopped doing his videos, I adhered to that rule. After some thought, though, I decided that it didn't make much sense. The Vim and Emacs cultures are different---you're shocked to hear it, I know---especially the way they use the editors. I used Vi/Vim for many years (longer than I've used Emacs) and I don't ever remember adding an extension script. With Emacs, on the other hand, I have several and wouldn't want to live without them.

      That's why I said "standard" packages are OK. Anyone can do it in 1 keystroke by writing some ad hoc Elisp that directly solves the problem, but that's not what we're after. Rather, I think we want to know how well you can meet the challenge with your normal Emacs environment. So basically, if you already have a package loaded and it's standard in the sense that it's loadable with ELPA, say, then I think it's fair game.

      All that said, there's nothing to say you can't try to solve the problem with a stock Emacs distribution. The idea of these challenges is to help us learn to use Emacs better and seeing a nice solution from someone using a package you don't have is part of that. I've certainly picked up packages when I saw solutions using them.

  4. Xah Lee says:

    5, using my own functions, and counting each function call as 1 keystroke.

  5. Tim Visher says:


    Challenge ID: 5192f96ad8df110002000002
    http://vimgolf.com/challenges/5192f96ad8df110002000002.yaml

    Keystrokes (9):

    <f3> ( M-f ) C-f <f4> <f4> <f4>

    Full command log:

    <f3> kmacro-start-macro-or-insert-counter
    ( self-insert-command
    M-f forward-word
    ) self-insert-command
    C-f forward-char
    <f4> kmacro-end-or-call-macro
    <f4> kmacro-end-or-call-macro
    <f4> kmacro-end-or-call-macro

    I'd encourage everyone to use VimGolf Minor Mode to participate as it produces this nice output. Of course it does need to be extended somewhat to support mode activations and such. That was me in Fundamental mode. Maybe I'll come up with something more creative later.

    • Isaac says:

      I arrived at the same ... not sure anyone can pass this with just the stock functionality in emacs

      • Fuco says:

        For example, you can do C-0 (zero) f4 to run the macro on the entire buffer, or C-3 f4 to run it three times. Saves you one keystroke

  6. Tim Visher says:

    And here's one with paredit active. Of course VimGolf Minor Mode collects the keystrokes required to get it going. But once it is it's only 6 keystrokes.


    Challenge ID: 5192f96ad8df110002000002
    http://vimgolf.com/challenges/5192f96ad8df110002000002.yaml

    Keystrokes (15):

    M-x p a r RET M-( C-M-u C-M-f C-f <f4> <f4>

    Full command log:

    M-x smex
    p self-insert-command
    a self-insert-command
    r self-insert-command
    RET ido-exit-minibuffer
    M-( paredit-wrap-sexp
    C-M-u paredit-backward-up
    C-M-f paredit-forward
    C-f forward-char
    <f4> kmacro-end-or-call-macro
    <f4> kmacro-end-or-call-macro

  7. Xah Lee says:

    turns out default GNU emacs can do in 7. Turn on electric-pair-mode first.

    Challenge ID: 5192f96ad8df110002000002
    http://vimgolf.com/challenges/5192f96ad8df110002000002.yaml

    Keystrokes (7):

    M-@ (

    Full command log:

    kmacro-start-macro-or-insert-counter
    M-@ mark-word
    ( self-insert-command
    right-char
    kmacro-end-or-call-macro
    kmacro-end-or-call-macro
    kmacro-end-or-call-macro

    • Xah Lee says:

      sorry for the spam. Forgot to encode angle brackets.

      Keystrokes (7):

      【f3】 M-@ ( 【right】 【f4】 【f4】 【f4】

      Full command log:

      【f3】 kmacro-start-macro-or-insert-counter
      M-@ mark-word
      ( self-insert-command
      【right】 right-char
      【f4】 kmacro-end-or-call-macro
      【f4】 kmacro-end-or-call-macro
      【f4】 kmacro-end-or-call-macro

      ----------------

      test 1

      test 2 <something>

  8. I can do it in 5 with paredit: C-( M-f C-( M-f C-(

Comments are closed.