215

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

289

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

7
  • 3
    I knew it would be easy... Mar 31, 2011 at 23:28
  • 51
    :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
  • 2
    % is optional since the default range is the entire buffer.
    – stillanoob
    Feb 6, 2019 at 11:38
45

You can also use:

:v/price/d

to delete lines.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.