Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim 8 - How do I re-number my list after reordering the list - manually or automatically?

Tags:

vim

sorting

Say I have a list like this:

1. apple
2. banana
3. orange
4. plum

Now I want to reorder them by deleting/cutting lines and pasting them in the right spot, but the numbers are no longer in order:

2. banana
1. apple
4. plum
3. orange

How can I re-number this list or have Vim handle a numbered list automatically? (I'll add my answer, but I believe there should be a faster way)

like image 809
wxz Avatar asked Dec 30 '25 21:12

wxz


1 Answers

NOTE: this answer assumes that you are doing that in a markdown context.

FWIW, Markdown renderers are generally expected to ignore the actual numbers in your numbered list except the first one, which is used as a hint for how the output will be numbered.

This list:

2. banana
1. apple
4. plum
3. orange

is rendered as:

  1. banana
  2. apple
  3. plum
  4. orange

list

<ol start="2">
<li>banana</li>
<li>apple</li>
<li>plum</li>
<li>orange</li>
</ol>

but this list:

1. banana
1. apple
4. plum
3. orange

is rendered as:

  1. banana
  2. apple
  3. plum
  4. orange

list

<ol>
<li>banana</li>
<li>apple</li>
<li>plum</li>
<li>orange</li>
</ol>

or maybe:

<ol start="1">
<li>banana</li>
<li>apple</li>
<li>plum</li>
<li>orange</li>
</ol>

If your ultimate goal is to render your markdown to something else (meaning that your *.md is only read when editing, not on its own), I would suggest you give up on proper ordering in your source and only use 1. as a hint for the renderer:

1. banana
1. apple
1. plum
1. orange

and let it do its magic:

  1. banana
  2. apple
  3. plum
  4. orange

list

<ol>
<li>banana</li>
<li>apple</li>
<li>plum</li>
<li>orange</li>
</ol>
like image 59
romainl Avatar answered Jan 02 '26 13:01

romainl