How do I insert the same text on multiple lines at once?
Answer
<C-v>jjjI
Explanation
Visual block mode combined with I lets you insert the same text at the beginning of multiple lines simultaneously. This is one of Vim's most impressive multi-cursor-like features, letting you edit many lines in a single operation.
How it works
- Press
<C-v>(Ctrl+v) to enter visual block mode - Use
jorkto extend the selection down or up across multiple lines - Press
I(uppercase) to enter insert mode at the left edge of the block - Type your text — it appears on the first line only while you type
- Press
<Esc>— the same text is inserted on every selected line
Example
Given the text:
first line
second line
third line
fourth line
Press <C-v> on the first line, then 3j to select all four lines. Press I, type // , and press <Esc>. The result:
// first line
// second line
// third line
// fourth line
All four lines now have // prepended.
Tips
- Use
Ainstead ofIto append text at the right edge of the block selection - Use
$beforeAto extend the selection to the end of each line, even if they have different lengths - Make sure you press
<Esc>(not<C-c>) to trigger the multi-line insertion —<C-c>cancels the repetition - Use
<C-v>withcto change (replace) a rectangular block of text - This is especially useful for commenting out code, adding indentation, or prefixing multiple lines with the same text
- On Windows or if
<C-v>is mapped to paste, use<C-q>instead to enter visual block mode