Global (:g) and Vglobal (:v)

"Run a command on every line that matches."

Keys: :g, :v

:g/pattern/cmd runs cmd on every line matching pattern. :v inverts (every NON-matching line). Combine with :d to delete-by-pattern, with :s to substitute-by-pattern, with :norm to 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.

Step 1 ยท
ex.global
Step 2 ยท :g/TODO/d Enter ยท All TODO lines deleted.
All TODO lines deleted.

:g/pat/cmd runs cmd on each matching line. :v/pat/cmd runs on every NON-matching line.

โ–ถ Try this in the simulator

Watch

See also: Substitute ({key::s}), Normal ({key::norm})