g CTRL-A in visual mode can accomplish that for you. It creates an incrementing sequence, incrementing a number on each line by a subsequent number.
As an example of using this to build a numbered list, somewhere in the middle of your file:
- Start visual block selection mode with
CTRL-V, at the first column of a line (or a different column if you want the numbers to be added later on, or including an indent.) Move to the last line of the block you want to number.
I0. <esc>, where the last key is the ESC key. You're entering insert mode with
I (more specifically, visual block insert), then inserting "zero", "dot", "space" (which are inserted to each line), and leaving insert mode with an ESC. When you leave insertion mode, Vim will add the text to each line in the block.
gv to recall your selection. This will select all "zeroes" you inserted.
- (Optional) Grow your insertion to include the whole numbers. It turns out right now all "zeroes" are covered, but if you're doing a further operation and have lines with two, three or four digit numbers, you'll want to make sure they're fully in the visual block. It's ok if there are further characters in the block. You can use
V here to switch to a linewise visual select, or you can keep in a visual block and use
$ to select all the way to the end of the line.
- Finally, use the
g CTRL-A command to increment the zeroes. You type a
g (lowercase "g", which will be pending), followed by a Control-A.
After this sequence, you'll end up with a numbered list, with a number on each line, starting at 1. Pretty handy stuff!
Side note: Beware if you use
screen, since Control-A is the default hot-key for it and might go to
screen and not Vim. The typical way to pass a Control-A to the application is to type Control-A, then "a". The default keybindings for
tmux are different, but using Control-A for
tmux is a popular choice, in which case Control-A then "a" is typically the way around that too.
You asked about deleting numbers from some lines, which should be easy to do using the usual deletion commands, such as
dW, also using visual block mode you can use
r + "space" to replace the numbers with spaces to keep the lines where they are.
If you further need to adjust the numbering, typically by decrementing the numbers on the lines that follow (so you're not skipping from, say, 8 to 12), you can then use the CTRL-X command, with a count, to subtract from the numbers in the lines that follow.
Simply select the lines that follow (visual line mode with
V is fine), then type, for example,
3 (if that is the number of lines you deleted numbers from), followed by
CTRL-X, which will then subtract 3 from the numbers in the lines of your visual selection.
g CTRL-A in visual mode is also useful if you want to number paragraphs, since it will skip lines with no numbers in them.
If you have text that looks like this:
0. This is a paragraph. It uses multiple
lines to make a point. There are 123
numbers 456 in the middle of the
0. This is another paragraph.
0. And a final one here.
You can then use visual block mode to select the first 6 columns (or even fewer) with the zeroes and use
g CTRL-A, which will renumber your paragraphs to
If you have more than 9 paragraphs, some will get misaligned (as you get to double digit numberings.) In that case, use visual block mode to select the text of the paragraphs themselves and use
< to re-align them.