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 :g and :v to execute commands on lines that match (or do not match) a pattern

  • Delete, 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 A

Tip: Use uppercase A to append to the register. Lowercase a would 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 3

  • normal! 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 line

  • exe 'normal! ...' — execute in Normal mode

  • fw — jump to "words"

  • yiw — copy the word

  • w — jump to next word

  • viw — select it

  • p — paste (swapping the words)

  • bbviwp — 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

Command
What it does

: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 @a to 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/command executes a command on every line matching the pattern; :v/pattern/command targets non-matching lines.

  • Use :g/pattern/y A (uppercase register) to append matching lines rather than overwriting.

  • Combine :g with :normal to run any Normal mode editing sequence on matching lines.

  • The global command can include conditional logic with if/endif for fine-grained control.

Exercises

  1. 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 contain ERROR.

  2. Collect matching lines into a register — Yank all lines containing TODO into register a, then paste them at the end of the file.

  3. Move lines to a specific location — Move all lines starting with # (comments) to the top of the file.

  4. Add text conditionally — Append the string -- reviewed to the end of every third line in the file.

  5. 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?