248

In vim I can delete all lines that contain the word "price" with this

:g /price/d

How can I delete all lines that do NOT contain the word "price"?

digitaljoel
  • 2,863

3 Answers3

344

You can use

:g!/price/d

to delete every line that doesn't contain "price"

As answered below, g! is an alias to v. this is equivalent to

:v/price/d

Yab
  • 4,043
57

You can also use:

:v/price/d

to delete lines.

MOHRE
  • 701
16
%!grep "price"

is another option that can be considerably faster than :v for large files.

Tested on Vim 7.4, Ubuntu 14.04, 1M line log file.

Lines that contain word: https://stackoverflow.com/questions/1725265/how-can-i-delete-all-lines-that-do-not-begin-with-certain-characters/42714334#42714334