How to Quickly Append Semicolons to Multiple Lines in Vim
This guide shows four efficient Vim techniques—using the dot command, column visual block mode, Ex commands, and macros—to append a semicolon at the end of each line in a multi‑line JavaScript snippet, reducing repetitive keystrokes and improving editing speed.
When editing a short JavaScript snippet in Vim, you may need to add a trailing semicolon to every line. Manually typing a semicolon at the end of each line is tedious, so this article presents four Vim‑based methods to automate the task.
Method 1: Use the dot (.) repeat command
Press $ to move the cursor to the end of the first line, then type a;<Esc> to append a semicolon.
Press j to move to the next line and repeat the previous step with $a;<Esc> .
Repeat step 2 for each subsequent line.
The . command repeats the last change, so after the first line you can use j$. to add a semicolon to the following lines, dramatically reducing keystrokes.
Method 2: Column visual block mode
Enter block‑visual mode with <Ctrl+v> (or <Ctrl+q> in MacVim/GVim), then move down two lines with jj and to the end of the lines with $ .
Press A; to enter insert mode and append a semicolon to all selected lines.
Press <Esc> to return to normal mode, completing the insertion.
This technique leverages block selection so the inserted text is applied simultaneously to every selected line.
Method 3: Use an Ex command
Determine the line range (e.g., lines x to y) and execute: :x,ynormal A; The normal Ex command runs normal‑mode keys on each line in the range, appending a semicolon at the end. Ex commands can also be combined with pattern matching, e.g., :g/pattern/normal A; to affect only lines matching a pattern.
Method 4: Record and replay a macro
On the first line, start recording with qa , then type A;<Esc> and stop recording with q .
Move to the second line with j and replay the macro with @a .
Move to the third line with j and replay the last macro with @@ .
Macros capture a sequence of keystrokes, allowing you to apply the same edit to multiple lines with a single command.
These four approaches—dot repeat, column visual block, Ex command, and macro—provide flexible ways to add semicolons efficiently in Vim, minimizing repetitive typing and improving workflow.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Aotu Lab
Aotu Lab, founded in October 2015, is a front-end engineering team serving multi-platform products. The articles in this public account are intended to share and discuss technology, reflecting only the personal views of Aotu Lab members and not the official stance of JD.com Technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
