VimGolf In Emacs: A Numbered List

The conventional wisdom is that Vim is much more efficient—at least in keystrokes—than Emacs. In working the various VimGolf challenges and reading (or watching) other peoples’ solutions I’ve found that that’s often true but not always. A non-trivial part of the time Emacs can do much better than Vim. This challenge is an example of that.

This VimGolf challenge is to convert a pandoc unordered list to a numbered list. That’s extraordinarily simple if we’re using Org mode but even treating the file as just text we can still do better. The challenge is to take the starting file

My cursor happens to be on this line

## Here is my unordered list
   * item 1
      + it has some sub-items
         - sometimes it has sub-sub-items
      - i list some information about it
   * here is another item
   * and another
      - with some random sub-item
      - maybe with something in **bold** to make asterisks suck
   * woohoo I'm listing stuff like mad
   * I'm a mad pandoc'ing fool
      - long-live **John MacFarlane**!
      + reasons why pandoc is awesome
         - there's just too many list
         - but i'll put some more sub-sub-items for example
         - superfluous - and + to make matching them a pain
   * woo look at these sexy bullet points
   * you almost don't want to turn them into integers
   * but then you do because you know it's the right thing to do

## Here's some other stuff in the file
   * blah blah blah
   * maybe I should go with #. but then it's not readable in txt format

and make part of it numbered.

My cursor happens to be on this line

## Here is my unordered list
   1. item 1
      + it has some sub-items
         - sometimes it has sub-sub-items
      - i list some information about it
   2. here is another item
   3. and another
      - with some random sub-item
      - maybe with something in **bold** to make asterisks suck
   4. woohoo I'm listing stuff like mad
   5. I'm a mad pandoc'ing fool
      - long-live **John MacFarlane**!
      + reasons why pandoc is awesome
         - there's just too many list
         - but i'll put some more sub-sub-items for example
         - superfluous - and + to make matching them a pain
   6. woo look at these sexy bullet points
   7. you almost don't want to turn them into integers
   8. but then you do because you know it's the right thing to do

## Here's some other stuff in the file
   * blah blah blah
   * maybe I should go with #. but then it's not readable in txt format

The best Vim solution is 20 keystrokes. Here’s my solution that does it in 12.

Ctrl+1 F3 Start macro with counter set to 1
Ctrl+s␠␠* Search for leading star
Return Exit incremental search
Backspace Delete star
F3. Insert counter and period
Ctrl+8 F4 Exit macro definition and execute it 8 times

Of course, if we treat the starting file as an Org buffer we can do it in 4.

Ctrl+3 Ctrl+n Move down to line of first star
Ctrl+c - Change to numbered
This entry was posted in General and tagged . Bookmark the permalink.

4 Responses to VimGolf In Emacs: A Numbered List

  1. Fuco says:

    you can even save one more stroke if you bind this useful function in isearch-mode (I have it on C-2… you can’t use prefix args there anyway)


    (defun my-isearch-kill-match ()
    "Kill the text last matched by isearch."
    (interactive)
    (isearch-exit)
    (kill-region (point) isearch-other-end))

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>