vimtricks.wiki Concise Vim tricks, one at a time.

How do I add line numbers to multiple lines using visual block?

Answer

<C-v>jjI1. <Esc>

Explanation

Visual block insert can add numbered prefixes to lines. Combined with g<C-a>, you can create an incrementing numbered list.

How it works

  1. Select lines with <C-v> and j
  2. Press I and type 0. (or 1. )
  3. Press <Esc> to apply to all lines
  4. Reselect with gv and press g<C-a> to make numbers increment

Example

Starting with:

apple
banana
cherry

After <C-v>2jI0. <Esc>gvg<C-a>:

1. apple
2. banana
3. cherry

Tips

  • g<C-a> creates incrementing sequences in visual mode
  • This technique works for any numbered list
  • Adjust the starting number as needed
  • Can also be done with :let i=1 | '<,'>g/^/s//\=i.'. '/|let i+=1

Next

How do you yank a single word into a named register?