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

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

  1. Press <C-v> to enter Visual Block mode
  2. Use j/k (or a count) to extend the selection down across the lines you want to edit
  3. Press I (uppercase) to enter insert mode at the start of the block
  4. Type the text you want to prepend
  5. 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, A appends at the actual end of the line rather than at the fixed column — useful for ragged-length lines
  • Combine with <C-v>G to select from the cursor to the last line of the file instantly

Next

How do I browse the full commit history of the current file using vim-fugitive?