How do I duplicate every non-blank line in a buffer with one Ex command?
Answer
:g/./t.\<CR>
Explanation
The :global command can apply an Ex action to every line that matches a pattern. Combining it with :copy (:t) gives you a compact way to duplicate matched lines without recording a macro or touching Visual mode. This is especially useful when you need a quick expansion pass, such as duplicating checklist items, log statements, or template lines while keeping blank-line structure intact.
How it works
:g/./t.
:g/./selects every line matching.(any non-empty line)t.copies each matched line to.(the current line), which means directly below itself- Because
:globalcomputes matches first, each original non-blank line is duplicated once
Example
Before:
alpha
beta
gamma
Run:
:g/./t.
After:
alpha
alpha
beta
beta
gamma
gamma
Tips
- Restrict to a range:
:10,40g/./t. - Use a tighter pattern, e.g.
:g/^\s*TODO/t.to duplicate only TODO lines - Use
:g/pattern/m$instead when you want to gather matches at the end instead of duplicating in place