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

  • 1
    Can this question be migrated to vi.stackexchange.com? I tried to flag it for migration but couldn't figure it out. – Mu Mind Dec 21 '15 at 16:40
  • @MuMind It's on topic here, so no need to migrate. "Don't migrate for the sake of migration. We only migrate questions because they are off-topic on the original site. It is perfectly possible for a question to be on-topic on multiple sites, but that is not a reason to migrate it elsewhere, unless the OP requests migration." Also, only SE employees can migrate after 60 days and it's very rare that they will (not even moderators can migrate an old question). – 8bittree Oct 17 '17 at 17:14

You can use


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

  • 2
    I knew it would be easy... – digitaljoel Mar 31 '11 at 23:28
  • 29
    :g! is also known as :v (akin to grep -v). – Chris Johnsen Apr 1 '11 at 3:17
  • 2
    What does the % do? – hippietrail Oct 30 '12 at 7:05
  • 3
    % is a special range in this context that means the whole file. See :help cmdline-ranges. – Heptite Oct 30 '12 at 18:05

You can also use:


to delete lines.

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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