Member-only story
The Vim Global Command
There are several modes inside of Vim (and Neovim). Each has a special purpose and use cases. The global command is one that can be very powerful for performing operations across a matched pattern. I’ll walk you through a few examples so you can start using the global command in your workflows.
The general idea is to use the below example to match some text in a buffer and then perform an operation on all matches.
Example::g/pattern/command
Delete Matching Lines
Let’s start with a basic example of deleting a bunch of lines in a file.
If you had the word “error” in a few places then you could you this g command to delete all lines that match.
:g/error/d
Deletes all lines matching error
Pretty straight forward, what about if you wanted to delete all the lines that did NOT have “error” in them?
Delete Non-Matching Lines
You can use almost the same command but instead use a g! instead of just g. This will invert the match and grab all lines that do NOT match.
:g!/error/d
Deletes all lines not matching error.










