1

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.

asked Feb 6, 2017 at 20:27
2
  • Does this list take up the whole buffer? That would make it easier Commented Feb 6, 2017 at 21:03
  • @DJMcMayhem no, it's part of a README I'm editing. Commented Feb 6, 2017 at 21:11

3 Answers 3

1

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.

answered Feb 7, 2017 at 17:39
Sign up to request clarification or add additional context in comments.

1 Comment

I love when an answer tells me to do less than I was doing before :).
1

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).'. '
answered Feb 6, 2017 at 21:02

Comments

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

answered Feb 6, 2017 at 20:59

2 Comments

Meninx what is Ctrl-a doing there? That's exactly what I was looking for :).
ctrl-a is adding +1 and ctrl-x is -1 in normal mode for numbers usually but you can use it also for alpha letters.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.