← All lessons

19. The Global Command

Step 5 of 10

Chaining global commands

Global commands can be chained in Vim. That means the Ex command you pass to a :global invocation can be another :global invocation. This sounds a bit crazy, but think about the following examples:

To give you some ideas, I’ve used this is the past to:

The only limitation with using the global command recursively is that you can only specify a range for the first invocation of :global. As in, you can’t do this:

:g/foo/-10,+10g/bar/d

Thankfully the situations where you’d ever need this are extremely rare.

Let’s give this a try in the editor. Try deleting all of the lines that contain foo, but not bar, using a chained global command in the editor.

Loading editor…