YPCrumble
YPCrumble

Reputation: 28702

Is there a simple way to edit a numbered list using vim markdown?

Let's say I have a numbered list like this in markdown:

1. This is a line of text.
2. This is a line of text.
3. This is a line of text.
4. This is a line of text.

Is there an easy way to insert a new line for number one and add one to all the other lines using vim? This is what I want to achieve:

1. This is a new line of text.
2. This is a line of text.
3. This is a line of text.
4. This is a line of text.
5. This is a line of text.

I want to be able to do this quickly - not having to record a macro for instance.

Upvotes: 1

Views: 508

Answers (3)

Chris
Chris

Reputation: 137067

A completely different approach would be not to worry about manually numbering at all. Markdown doesn't care what numbers are in your source file, so

1. Foo
1. Bar
1. Baz

Renders as

  1. Foo
  2. Bar
  3. Baz

I normally number all of my ordered lists with 1. and let the Markdown processor take care of actual numbering.

Upvotes: 1

  • This: You can use the command below after selecting those lines by V4j starting from first line:

:'<,'>s/\d/\=submatch(0)+1/ | normal! goO1. This is a new line of text.

  • Or: Put the cursor on first line and type:

O0. This is a new line of text.Esc0Ctrl-v4jCtrl-a


O : letter o in capital case (shift+o)

0 : digit 0

Upvotes: 1

DJMcMayhem
DJMcMayhem

Reputation: 7679

There's a couple ways you could do this. Probably the simplest way is to add a newline starting with a '0.'. Then, when you are happy with this list you can increment every number with vG<C-a>. (Of course, it's not the whole buffer you could use a different movement such as ip or })

If you are going to add a line many times, it might be easier to add all of the numbers at the end when you are done editing. This could be done by visually selecting the lines you want and doing g<C-a>.

I don't remember exactly when these feature's were added, but this requires a relatively new version of vim. It works for me in 8, and I remember this working for 7.4 with the right patches.

If you are on an older version of vim, you could select the lines you want and use a substitute command to add the appropriate numbers. For example:

:s/^/\=(line('.') - line("'<") + 1).'. '

Upvotes: 1

Related Questions