Using vim to delete all lines except those that match an arbitrary set of strings
The :global
command that you reference in your question actually doesn't just take literal strings, it handles any regular expression. So, you just need to come up with one that has two branches, one for John
and one for Dave
. Voila:
:g!/Dave\|John/d
Note that this simplistic one would also match Johnny
; you probably want to limit the matches to whole keywords:
:g!/\<\(Dave\|John\)\>/d
Regular expressions are a powerful feature of Vim; it's worthwhile to learn more about them. Get started at :help regular-expression
.
Following should do it
:v/\v(Dave|John)/d
Breakdown
:v matches all lines not containing the search expression
/\vDave|John search expression
/d execute delete on all those lines