Thursday, August 2

numeric lists with visual select in vim

I was messing around with some list markup a minute ago, and stumbled across a Vim feature I didn’t know about.

I knew that you can place the cursor on a number, and use Ctrl-A to increment the number or Ctrl-X to decrement it.

I was curious what would happen if I did Ctrl-V and made a visual selection of the first character in a block like this one, then hit Ctrl-A:

2. foo
   bar

3. baz

4. bif

I was hoping for this:

3. foo
   bar

4. baz

5. bif

What I wound up with instead was this:

3. foo
4  bar
5
4. baz
5
5. bif

I was pretty confused for a minute, but eventually I realized that it had filled in the whitespace I’d highlighted by incrementing the previous number in the sequence. The contents of :help Ctrl-A are a little bit misleading:

{Visual}g CTRL-A        Add [count] to the number or alphabetic character in
                        the highlighted text. If several lines are
                        highlighted, each one will be incremented by an
                        additional [count] (so effectively creating a
                        [count] incrementing sequence).  {not in Vi}
                        For Example, if you have this list of numbers:
                                1. ~
                                1. ~
                                1. ~
                                1. ~
                        Move to the second "1." and Visually select three
                        lines, pressing g CTRL-A results in:
                                1. ~
                                2. ~
                                3. ~
                                4. ~

…it turns out that in fact, if you hit g Ctrl-A, what you get is:

3. foo
   bar

5. baz

7. bif

I’m not entirely sure if I know what the hell is going on here, but it maps neither to the docs nor to anything that quite makes sense as a feature to me.

Anyway, the basic underlying behavior is useful for defining list elements.

p1k3 / wip / vim_vertical_list_thing