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

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

  1. Press <C-v> (Ctrl+v) to enter visual block mode
  2. Use j or k to extend the selection down or up across multiple lines
  3. Press I (uppercase) to enter insert mode at the left edge of the block
  4. Type your text — it appears on the first line only while you type
  5. 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 A instead of I to append text at the right edge of the block selection
  • Use $ before A to 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> with c to 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

Next

How do I edit multiple lines at once using multiple cursors in Vim?