235

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"?

0

3 Answers 3

318

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

8
  • 3
    I knew it would be easy... Mar 31, 2011 at 23:28
  • 57
    :g! is also known as :v (akin to grep -v). Apr 1, 2011 at 3:17
  • 3
    What does the % do? Oct 30, 2012 at 7:05
  • 4
    % is a special range in this context that means the whole file. See :help cmdline-ranges.
    – Heptite
    Oct 30, 2012 at 18:05
  • 3
    % is optional since the default range is the entire buffer.
    – stillanoob
    Feb 6, 2019 at 11:38
51

You can also use:

:v/price/d

to delete lines.

2
14
%!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

1
  • 1
    Super fast and a marvelous solution
    – zardilior
    Feb 20, 2022 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .