How do I insert text at the beginning of multiple lines at once using visual block mode?
Answer
<C-v>I
Explanation
Visual block mode's I command lets you type once and have the text inserted at the cursor column across all selected lines simultaneously. This is one of the fastest ways to prepend comment markers, indentation, or any prefix to a contiguous block of lines.
How it works
- Press
<C-v>to enter Visual Block mode - Use
j/k(or a count) to extend the selection down across the lines you want to edit - Press
I(uppercase) to enter insert mode at the start of the block - Type the text you want to prepend
- Press
<Esc>— Vim replays the insertion on every line in the block
Use A instead of I to append at the end of the block selection.
Example
Starting text (cursor on the f of foo):
foo
bar
baz
Press <C-v>2jI// <Esc>:
// foo
// bar
// baz
Tips
- The
<Esc>after typing is what triggers the multi-line replay — if you exit insert mode any other way (e.g.<C-c>) you may only see the change on the first line - On lines shorter than the block column,
Aappends at the actual end of the line rather than at the fixed column — useful for ragged-length lines - Combine with
<C-v>Gto select from the cursor to the last line of the file instantly