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

How do I prepend text to every line in a visual block selection?

Answer

I{text}<Esc>

Explanation

When you need to add the same prefix to many adjacent lines, Visual Block insert is faster and safer than repeating macros or substitutions. This pattern is ideal for commenting blocks, adding list markers, or inserting a log prefix on aligned lines. Experienced users often reach for :substitute, but block insert is more direct when the change is column-oriented.

How it works

  • Enter Visual Block mode with <C-v>.
  • Select a vertical block spanning the target lines (often the first column or first non-blank column).
  • Press I to start insert at the beginning of the block.
  • Type your prefix text.
  • Press <Esc> once; Vim applies the inserted text to every selected line.

The key detail is that replication happens on <Esc>, not while typing. If your lines have uneven indentation, select the exact column where you want the prefix to land.

Example

Given:

alpha
beta
gamma

Select the first column across all three lines with Visual Block, then run:

I- <Esc>

Result:

- alpha
- beta
- gamma

Tips

  • Use A{text}<Esc> in Visual Block mode to append at the right edge of the block.
  • Combine with motions like j, k, and % while the block is active to expand selection quickly.
  • If placement looks off with tabs, enable :set list temporarily to inspect real columns before inserting.

Next

How do I debug a Vim macro one command at a time?