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
- Select lines with
<C-v>andj - Press
Iand type0.(or1.) - Press
<Esc>to apply to all lines - Reselect with
gvand pressg<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