A Word Frequency VimGolf in Emacs Challenge

Here’s an interesting VimGolf challenge that nicely demonstrates the power of CUA selection mode. Given the word frequency chart

     align here
the        56271872
of        33950064
and        29944184
to        25956096
in        17420636
I        11764797
that        11073318
was        10078245
his        8799755
he        8397205
it        8058110

align the frequency counts so that it looks like

     align here
the  56271872
of   33950064
and  29944184
to   25956096
in   17420636
I    11764797
that 11073318
was  10078245
his  8799755
he   8397205
it   8058110

My first thought was to use align-regexp but that command doesn’t have a shortcut key sequence and the best regular expression I could find was [1-9]. A pretty nice solution but it certainly won’t win on keystrokes.

After messing around a bit, I came up with a solution of 4 keystrokes using the CUA rectangle functionality.

Meta+m Move to the a in align
Ctrl+Return Invoke rectangle mode
Meta+} Move to end of buffer
Meta+a Align the rectangle

Notice that the trailing digits of a few of the frequency counts aren’t in the rectangle but they get dragged along anyway. I’ve only been using CUA rectangle mode for a short while but I am continually amazed at how powerful it is. Certainly this problem doesn’t look like a rectangle problem but CUA rectangle mode solves it very handily anyway.

Sadly, those darn Vim people were able to solve this in 3 keystrokes so if you can shave off a stroke or two be sure to leave a comment. I don’t think you can squeeze any more keystrokes out of the rectangle strategy so you’ll probably need something entirely different.

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

3 Responses to A Word Frequency VimGolf in Emacs Challenge

  1. Cezar says:

    C-x h (select the whole buffer)
    M-x align-regexp (not sure if this is bound to something by default)

    • Cezar says:

      C-x h (select the whole buffer)
      M-x align-regexp (not sure if this is bound to something by default)
      SPACE
      RETURN

      (my previous comment got eaten by formatting, sorry)

      • jcs jcs says:

        There’s no default key sequence for align-regex but smex could probably deal with that to keep the keystroke count down. The main problem is that using a regexp of SPACE puts an extra space between the columns. You can see this in the that 11073318 column.

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>