Web development and useful information

Nathan's Blog

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 to Dan Herbert Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress & Theme by Anders Norén