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.
-
Does this list take up the whole buffer? That would make it easierDJMcMayhem– DJMcMayhem2017年02月06日 21:03:06 +00:00Commented Feb 6, 2017 at 21:03
-
@DJMcMayhem no, it's part of a README I'm editing.YPCrumble– YPCrumble2017年02月06日 21:11:40 +00:00Commented Feb 6, 2017 at 21:11
3 Answers 3
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
- Foo
- Bar
- Baz
I normally number all of my ordered lists with 1. and let the Markdown processor take care of actual numbering.
1 Comment
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).'. '
Comments
- This: You can use the command below after selecting those lines by
V4jstarting 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
2 Comments
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.