The Global Command
The :global command — or simply :g — is one of Vim's hidden gems. Many Vim users go years without learning it, and when they finally do, they wonder how they ever lived without it.
The idea is beautifully simple: find all lines that match a pattern, then execute a command on each of them. Want to delete every line containing "DEBUG"? Copy all TODO comments into a register? Add text to every third line? The global command handles all of these with a single line. It builds directly on the search and regex patterns covered in the previous chapter (see Search, Replace & Regular Expressions).
Its counterpart :g! (or :v) does the inverse — it operates on lines that do not match the pattern. Together, they give you surgical control over your text.
What you'll learn in this chapter:
Use
:gand:vto execute commands on lines that match (or do not match) a patternDelete, move, copy, and yank lines selectively based on regex patterns
Combine the global command with substitution, Normal mode commands, and expressions
Apply conditional logic within the global command to handle complex editing tasks
Basic Syntax
:[range]g/pattern/command
:[range]v/pattern/command " inverted (lines NOT matching)Common Uses
Copy Lines Matching a Pattern
" Copy all TODO lines to register a
:let @a=''
:g/TODO/yank ATip: Use uppercase
Ato append to the register. Lowercaseawould overwrite each time. To copy to clipboard::let @+=@a
Note: The clipboard register
+does not support appending mode. Writing:g/pattern/yank +will overwrite the clipboard with only the last match. To collect multiple matches, always yank into a named register (e.g.,A) first, then copy it to clipboard::let @+=@a
Delete Lines
Move and Copy Lines
Conditional Changing
Add text to every third line:
Explanation:
:g/^/— for every line (every line starts with^)line('.')— current line number% 3 == 0— divisible by 3normal! A— append at end of line
Erase Even or Odd Lines
Toggling Words with Global Command
Exchange the last two words on every line:
Breakdown:
g/./— for every non-empty lineexe 'normal! ...'— execute in Normal modefw— jump to "words"yiw— copy the wordw— jump to next wordviw— select itp— paste (swapping the words)bb→viw→p— go back and swap the other word
Incrementing Numbers
Add sequential IDs to a YAML-like structure:
This creates a counter, increments it for each match, and appends the value after id:.
Decrease Numbers on Lines
Add Blank Lines Where Missing
Global Command Quick Reference
:g/pattern/d
Delete matching lines
:v/pattern/d
Delete non-matching lines
:g/pattern/y A
Append matching lines to register a
:g/pattern/m $
Move matching lines to end of file
:g/pattern/t 0
Copy matching lines to beginning
:g/pattern/normal @a
Run macro a on matching lines
:g/^/m 0
Reverse all lines in file
:g/pattern/s/old/new/g
Substitute only on matching lines
Tip: The global command pairs powerfully with macros (see Macros) — use
:g/pattern/normal @ato run a recorded macro on every matching line.
Summary
The :global command is one of Vim's most versatile tools for structured text manipulation. By combining a pattern with any Ex command, it lets you selectively delete, move, copy, substitute, or run Normal mode commands on matching lines. Its inverse, :v (or :g!), operates on non-matching lines. When combined with counters, conditional logic, and expressions, the global command can handle editing tasks that would otherwise require external scripting.
Key takeaways:
:g/pattern/commandexecutes a command on every line matching the pattern;:v/pattern/commandtargets non-matching lines.Use
:g/pattern/y A(uppercase register) to append matching lines rather than overwriting.Combine
:gwith:normalto run any Normal mode editing sequence on matching lines.The global command can include conditional logic with
if/endiffor fine-grained control.
Exercises
Delete lines matching a pattern — Given a log file, delete all lines that contain the word
DEBUG. Then undo and instead delete all lines that do not containERROR.Collect matching lines into a register — Yank all lines containing
TODOinto registera, then paste them at the end of the file.Move lines to a specific location — Move all lines starting with
#(comments) to the top of the file.Add text conditionally — Append the string
-- reviewedto the end of every third line in the file.Combine global with substitution — On every line that contains the word
price, double the first number that appears on that line using the expression register.
Last updated
Was this helpful?