Sitemap
Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

The Vim Global Command

3 min readJan 7, 2025

--

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.

Press enter or click to view image in full size

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.

--

--

Andrew Courter
Andrew Courter

Written by Andrew Courter

Manager of Software Engineering. Writes on Neovim, programming, and building great software. https://andrewcourter.com