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

How do I insert the same text at the start (or end) of multiple lines simultaneously?

Answer

<C-v>{motion}I{text}<Esc>

Explanation

Visual block mode (<C-v>) lets you select a rectangular region across multiple lines. Using I (insert before the block) or A (append after the block) while in visual block mode inserts text on every selected line simultaneously — making it the fastest way to add prefixes like comment markers or list bullets to a range of lines.

How it works

  • <C-v> — enter visual block mode
  • {motion} — extend the selection downward (e.g., 3j for 4 lines, G to end of file)
  • I — enter insert mode before the left edge of the block on all selected lines
  • {text} — type the text you want inserted (you only see it on one line while typing)
  • <Esc> — the text is replicated to every line in the selection

Use A instead of I to append after the right edge of the block on each line.

Example

To comment out three lines of JavaScript by prepending // :

const a = 1
const b = 2
const c = 3
  1. Place cursor on const a = 1
  2. <C-v>2j — select all three lines as a block
  3. I// <Esc> — insert // before each line

Result:

// const a = 1
// const b = 2
// const c = 3

Tips

  • On lines of different lengths, A appends after the longest line's column on shorter lines — use $ before A to append after each line's own end: <C-v>{motion}$A{text}<Esc>
  • To delete a visual block (e.g., remove a leading // ), select the two-character block with <C-v> and press d
  • In Neovim, changes appear on all lines in real time; in Vim, you see them only after <Esc>

Next

How do I build a macro programmatically using Vimscript instead of recording it?