How do I append text to the end of multiple lines that have different lengths?
Answer
<C-v>jj$A;
Explanation
Visual block mode normally selects a fixed-width column, which makes appending tricky when lines have different lengths. The key insight is using $ after entering visual block mode — this extends the selection to the end of each line regardless of length. Then A enters insert mode after the last character on every selected line, and whatever you type appears at the end of all of them.
How it works
<C-v>— enters visual block modejj(or any vertical motion) — selects multiple lines$— extends the selection to the end of each line, even if they differ in lengthA— appends after the selection on every line- Type your text (e.g.,
;), then press<Esc>— the text appears at the end of all selected lines
Example
Before: After:
let x = 1 let x = 1;
let name = "foo" let name = "foo";
let y = 42 let y = 42;
The ; is appended to the end of each line, regardless of how long they are.
Tips
- Without
$, visual block mode selects a fixed column —Awould then insert at that column position, not the end - Use
Iinstead ofAto insert at the beginning of the block selection - This technique works for appending any text: commas, closing brackets, comments, etc.
- Combine with
ggandGto select all lines:<C-v>gg$Aor<C-v>G$A