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

How do I append text at the end of lines with different lengths using visual block?

Answer

<C-v>j$A;<Esc>

Explanation

When lines have varying lengths, a normal visual block selection stops at the shortest line. Using $ in visual block mode extends the selection to the end of every line regardless of length, allowing you to append text at the true end of each line.

How it works

  • <C-v> — enter visual block mode
  • j (repeated) — extend selection down
  • $ — extend selection to end of each line (ragged right edge)
  • A — append after the selection on every line
  • Type your text, then <Esc> — applies to all lines

Example

Before:
foo
foobar
foo baz quux

After (appending ';'):
foo;
foobar;
foo baz quux;

Tips

  • Without $, block selection is rectangular and won't reach the end of longer lines
  • This is ideal for adding semicolons, commas, or closing brackets to lines of varying length
  • Use I instead of A to insert at the beginning of the block selection
  • Combine with gv to reselect and apply another transformation

Next

How do I return to normal mode from absolutely any mode in Vim?