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

How do I append text to the end of multiple lines of different lengths?

Answer

<C-v>$A

Explanation

When you need to append text to the end of several lines that have different lengths, visual block mode with $ is the key. A regular block selection (<C-v>) selects a fixed-width rectangle, which doesn't reach the end of shorter or longer lines. By pressing $ after entering visual block mode, Vim extends the selection to the end of each line regardless of length, and A then appends at each line's end.

How it works

  • <C-v> — enter visual block mode
  • Select the lines you want (e.g., j to go down)
  • $ — extend the block selection to the end of each line
  • A — enter insert mode at the end of each selected line
  • Type your text and press <Esc> — the text appears at the end of every selected line

Example

Before (with lines of varying length):

short
medium text
a much longer line here

After <C-v>2j$A;<Esc>:

short;
medium text;
a much longer line here;

All lines get a semicolon appended, regardless of length.

Tips

  • Without $, block mode selects a fixed column width — shorter lines get nothing appended, or text gets inserted at the wrong position
  • This is essential for adding trailing commas, semicolons, or closing delimiters to code blocks
  • Works with I at the beginning too: <C-v>jjI// <Esc> comments out lines

Next

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