How to do an inverse search in Vim? (i.e., get lines not containing pattern)

:g/pattern/

matches all the lines were pattern is found.

:v/pattern/

does the opposite. See :h global for more details.

You can use it like this:

:v/pattern/norm Ipattern not found - <CR>

to prepend "pattern not found - " to every line that doesn't have "pattern" or

:v/pattern/s/nrettap/pattern

to replace "nrettap" with "pattern" on every line that doesn't have "pattern".

Contrived examples, yes.


To search for the lines not containing foo, for example, do:

/^\(\(.*foo.*\)\@!.\)*$

Source: http://vim.wikia.com/wiki/Search_for_lines_not_containing_pattern_and_other_helpful_searches


Using the :v commandEdit The traditional approach to find lines not matching a pattern is using the :v command:

:v/Warning/p

A neat trick when working with a large log file where you want to filter out as many irrelevant lines as possible before you get started on your real search is to save the file under a temporary name and delete all non-matching lines there:

:sav junk.log
:v/warning/d

You are now are editing a clone of your original file with all lines not matching "warning" removed and you can edit it at will.

Ref: https://vim.fandom.com/wiki/Search_for_lines_not_containing_pattern_and_other_helpful_searches