Nathan's Blog

Web development and useful information

Vim: delete lines that don’t match pattern

Ex command :v to the rescue!

:v/pattern/d

Using :g! will give you the same result as using :v.
:g finds lines that do match a pattern.

I had a large list of locations. I wanted to keep only the locations containing hyphens and delete the rest so I used this: :v/-/d

:v – find lines not matching your pattern
d – delete matches


Recently Loved Articles

Previous

Unit test private methods in PHP

Next

How to protect your HSBC Secure Key

2 Comments

  1. Dan Herbert

    Nice!

    I always forget about that one. I usually do (for your scenario with hyphens):
    :%!grep -v –

Leave a Reply

Powered by WordPress & Theme by Anders Norén