In Vim, g
refers to the global command, an extremely powerful and versatile feature used to execute an Ex command on every line that matches a specified pattern throughout a file.
Understanding the Global Command (:g
)
The global command, invoked as :g
, allows users to perform operations on multiple lines simultaneously, making it indispensable for advanced text manipulation and refactoring tasks. Its core function is to first identify lines matching a regular expression and then apply a subsequent command to each of those identified lines.
Syntax and Operation
The general syntax for the global command is:
:g/{pattern}/{command}
{pattern}
: This is a regular expression (regex) that Vim uses to search for lines. Only lines that contain a match for this pattern will be processed.{command}
: This is an Ex command (also known as a command-line mode command) that will be executed on each line identified by the{pattern}
.
Practical Examples and Insights
The flexibility of the :g
command comes from its ability to combine with various patterns and commands:
- Targeting Non-Empty Lines: A common and useful pattern is
/./
. This regular expression matches any line that contains at least one character, effectively targeting all "non-empty lines" in your file. For instance, lines containing text like "const" or "console" would be matched, while completely blank lines would be ignored. - Executing Normal Mode Commands: You can run Normal mode commands on matched lines by using the
:normal
Ex command. For example,:g/{pattern}/normal A;
would append a semicolon (;
) to the end of every line that matches{pattern}
. Here,A
moves to the end of the line and enters insert mode, and;
is the character inserted before Vim automatically returns to Normal mode.
Here are more common applications of the global command:
- Deleting Specific Lines:
:g/Error/d
: Deletes every line that contains the word "Error".:g/^\s*$/d
: Deletes all entirely empty or whitespace-only lines.
- Commenting Out Lines:
:g/^/normal I//
: Inserts//
at the very beginning of every line in the file, effectively commenting them out for many programming languages.
- Appending or Prepending Text:
:g/function/normal A;
: Appends a semicolon to the end of every line that contains the word "function".
- Substituting Text Globally (on matching lines):
:g/todo/s//DONE/g
: First, it finds all lines containing "todo". Then, on each of those lines, it substitutes all occurrences of "todo" with "DONE". This differs from a simple:%s/todo/DONE/g
which would perform the substitution on every line.
Related Commands
The global command often works in conjunction with or has a complementary command:
Command | Description | Example | Effect |
---|---|---|---|
:g |
Global command: Executes a command on lines that match a pattern. | :g/DEBUG/d |
Deletes all lines containing "DEBUG". |
:v |
Inverse global command (vglobal): Executes a command on lines that do not match a pattern. | :v/main_func/d |
Deletes all lines not containing "main_func". |
By mastering the :g
command and its variations, Vim users can achieve remarkable efficiency in managing and transforming text, making it a cornerstone for productive development and content creation workflows.