← All lessons

19. The Global Command

Step 4 of 10

A better definition

The definition from earlier isn’t quite the full story. A better definition would be:

The global command lets you run an Ex command from each matching line in a range, defined by a regular expression.

The difference is subtle, but there are two key bits:

How does it work?

The algorithm is quite simple:

This two-pass algorithm means that Vim won’t get confused about which lines to act on when, for example, adding and deleting lines with :global.

Try the global command again on the left. Notice how it leaves your cursor on the last regex match in the buffer! That’s because the global command is physically moving your cursor to each match.

Loading editor…