Mickey's VimGolf 4

Mickey's getting into VimGolf mode with another solution to a VimGolf Challenge. I was looking at this same challenge for a possible post but Mickey beat me too it and with a very nice strategy. The challenge is to take a multi-line list of words and transpose it. Specifically, take

ultricies, vehicula, felis, sed, auctor, aenean, euismod, semper, quam, dapibus
nibh, consequat, consequat, maecenas, sit, amet, mauris, justo, quis, porttitor
curabitur, pharetra, euismod, orci, sit, amet, ullamcorper, mi, tincidunt, et
vitae, lorem, at, mi, feugiat, convallis, ac, eget, dui, fusce
blandit, iaculis, nulla, sit, amet, dolor, nec, est, ornare, volutpat

and turn it into

ultricies  nibh       curabitur    vitae      blandit
vehicula   consequat  pharetra     lorem      iaculis
felis      consequat  euismod      at         nulla
sed        maecenas   orci         mi         sit
auctor     sit        sit          feugiat    amet
aenean     amet       amet         convallis  dolor
euismod    mauris     ullamcorper  ac         nec
semper     justo      mi           eget       est
quam       quis       tincidunt    dui        ornare
dapibus    porttitor  et           fusce      volutpat

Mickey tends to concentrate more on technique than minimal keystrokes, which makes his solutions more useful outside of the narrow confines of VimGolf in Emacs. Still, it's nice to see how efficient we can be in keystrokes if only to keep those Vim people in their place. Here, then, is my solution to the problem, which follows Mickey's general strategy.

It helps to start off in an Org mode buffer because a shortcut key sequence for org-table-convert-region is available. Unfortunately, there is not shortcut for org-table-transpose-table-at-point but we can save a bunch of keystrokes with smex.

Ctrl+x h mark buffer
Ctrl+c | turn region into table
Meta+xo-t-t-t-a-pReturn transpose table
Meta+< beginning of buffer
Ctrl+Meta+% query regexp replace
| \| +|$Return replace |'s and surrounding spaces
Return with nothing
! replace all

That's 30 keystrokes, which isn't bad. The best Vim solution was 29 (with the usual rules about not counting saving the buffer) so we're desperately in need of someone to shave off another stroke if we want to keep the Vimers from getting too smug. Of course someone could complain that smex isn't part of stock Emacs but it really should be and since every serious Emacs user has it installed, I don't think it's breaking the rules—too much—to allow it.

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

3 Responses to Mickey's VimGolf 4

  1. twiki says:

    I can not understand ... when I give the word table o-t-t-t-a-p
    Emacs me always the error "apply: Symbol's function definition is void: mapcar*"
    Something is missing? (I've Emacs 24.2.1 for Windows and Org 7.9.3d)

    • jcs jcs says:

      You need smex for that to work. Also make sure that smex has flex (fuzzy) matching enabled. That's the default so simply loading smex should do the trick. You really want smex anyway so give it a try.

  2. Hi,

    I think that it's possible without smex. Ido mode in emacs core can do that too.

    (setq ido-enable-flex-matching t)

    and you have it !

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>