Global (g)

global

Syntax:

[line_range]g/pattern/more_editor_commands

[line_range]g^/pattern/more_editor_commands

Description:

The Global command checks each line in the indicated line range for the presence of the indicated pattern. The first form (g) marks lines which contain the pattern while the second form (g^) marks lines which do not contain the pattern. The Global command then executes the following commands for each marked line, with the current line set at the marked line before executing. If no line range is specified then all lines are searched for the pattern. For example:

g/^Comment/d

would remove all lines containing the string "Comment" at the beginning of the line. To print them before deleting use:

g/^Comment/pd

To Change all occurrences of the string "minimum" to "maximum" and print the changed lines, issue a:

g/minimum/s//maximum/p

To delete all lines which don't end in the string ".c" issue a:

g^/\.c$/d

To append the line "-------" after each line in the buffer, issue a:

g/^/a  -------

To reverse the order of lines in your buffer, issue a:

g/^/.m0

Finally, to print the lines around each line containing the string "function", issue a:

g/function/.-5,.+5p

Current line:

When the Global command is finished, the current line has the value it had after the last command executed during the Global operation.

Condition register:

When the Global command is finished, the condition register has the value it had after the last command executed during the Global operation.