Step 2 of 10

19. The Global Command

The inverse global command

The global command has a cousin, called the inverse global command or :vglobal, that does something slightly different.

The global command finds all of the lines in a buffer that match a regular expression and performs an Ex command on them.

The inverse global command finds all of the lines in a buffer that do not match a regular expression and performs an Ex command on those.

You can think of the global command as selecting the lines that match a regular expression, whereas the inverse global command filters out the lines that match.

It works in the exact same way as :global/:g — you just use :vglobal/:v instead.

Let’s try this in the editor. Once again, a secret message is hidden among the HAYSTACK lines. This time, the secret message lines aren’t uniquely identified by anything — so instead of selecting them directly, you’ll filter out the HAYSTACK lines and print whatever’s left. As before, the solution waits at the very bottom of the buffer if you get stuck.

  1. Filter out the haystack with :v/HAYSTACK Type :v/HAYSTACK and hit Enter — everything else will print. Hit Enter again to put the output away.
Filter out the haystack with :v/HAYSTACK Type :v/HAYSTACK and hit Enter — everything else will print. Hit Enter again to put the output away.

Last one — goal check runs when it's done

Loading editor…