Skip to content

Fun with Vimgolf 1: Alphabetize the Directory

by mickey on October 29th, 2011

I’ve been having a lot of fun with vimgolf, a site where vim users try to complete certain tasks (sort this, format that, scrub this data, etc.) in as few keys as possible. It’s an interesting site and the problem set is varied and a good source of inspiration for Emacs blog posts. I learned about this site from Jon’s Emacs blog and I’ve been having a fun time solving the problems in Emacs.

It’s obvious to most of us that to attempt a “fewest possible keystrokes” exercise in Emacs will invariably end in tears, as we can’t compete against vim in that regard; but with that said, we do have a lot of tricks up our sleeve.

In reading — or attempting to, more like — the “solutions” I’ve made a few observations about most of them: they are tailored to the input, and that input only; thus, it wouldn’t necessarily scale to larger problem sets or even smaller ones. I’m going to try to solve these problems so they’re technically correct — the best kind of correct — and thus work with larger or smaller input.

Alphabetize the Directory

Link to challenge.

Put the contacts and their information in alphabetical order.

So the task is simple. We need to sort by a key (the name heading), preserving the details associated with each contact.

The easiest way to solve this is to imagine the input below as an org-mode file, so that’s what we’ll do:

M-x org-mode and org mode immediately picks up on the file contents and collapses the headings.

Next, we need to sort the items, whilst we preserve the order of the subitems, and this is done by first moving to the heading level you want to sort by (in this case it’s the Smith, George heading) and invoking M-x org-sort and selecting a for alphabetic sort.

Org mode will sort the file and the output should match the expected vim golf output. And that’s it.

Input

Expected Output

5 Comments
  1. Thanks for org-sort! learned a new thing.

  2. If you trim off the leading whitespace temporarily with a rectangle selection, then “M-x sort-paragraphs” also does the job nicely.

  3. I recently created a small game for emacs for this kind of challenges. I hadn’t heard about codegolf/vimgolf/etc at the time, but if I had I would probably have called it emacsgolf. However, at the moment it is called puzzlEmacs and you can find it at http://www.lysator.liu.se/~ehliar/puzzlemacs/ if you like this kind of challenge.

    I haven’t updated in a while since I haven’t found the inspiration for any new kind of levels yet…

Trackbacks & Pingbacks

  1. Mickey Plays VimGolf In Emacs | Irreal

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS