Global (:g) and Vglobal (:v)
"Run a command on every line that matches."
:g/pattern/cmdrunscmdon every line matchingpattern.:vinverts (every non-matching line). Combine with:dto delete-by-pattern, with:sto substitute-by-pattern, with:normto do anything.
Global and Vglobal
:g/pattern/cmd is read as: for every line matching pattern, run cmd. It's how you do bulk operations gated by a search.
| Command | Effect |
|---|---|
:g/TODO/p |
Print every line containing TODO |
:g/TODO/d |
Delete every line containing TODO |
:v/TODO/d |
Delete every line not containing TODO |
:g/^$/d |
Delete all blank lines |
:g/^/m0 |
Reverse the file (move every line to top) |
:g/foo/s//bar/g |
Substitute on matching lines |
:g/^class /norm o pass |
On every class declaration, open new line + insert pass |
Worked example โ :g/TODO/d
Run an Ex command on every matching line.
:g/pat/cmd runs cmd on each matching line. :v/pat/cmd runs on every NON-matching line.
Watch
See also: Substitute (:s), Normal (:norm)