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

How do I append a semicolon to every non-blank line with one Vim command?

Answer

:g/[^[:space:]]/normal! A;\<CR>

Explanation

When you need to patch many lines at once, :g with :normal! is often faster and safer than recording a macro. This pattern runs a Normal-mode edit only on lines that match a filter, so you can bulk-edit without touching blank lines or comments you intentionally excluded.

How it works

  • :g/[^[:space:]]/ selects lines containing at least one non-whitespace character
  • normal! executes a Normal-mode command literally so mappings cannot interfere
  • A; jumps to end of each matched line and appends a semicolon

Because this executes line-by-line, it scales well for config files, SQL snippets, or refactors where you need consistent end-of-line punctuation quickly.

Example

Before:

const a = 1

const b = 2
const c = 3

Run:

:g/[^[:space:]]/normal! A;

After:

const a = 1;

const b = 2;
const c = 3;

Tips

  • Swap the pattern for something narrower, like ^\s*let, to target only specific statements.
  • Prefer normal! over normal for predictable behavior across different Vim setups.

Next

How do I run a substitution across the arglist and write only modified files?