Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Vim: Delete every line in the file that does not match a pattern

with 2 comments

I realized the power of :g much later than many other commands in vim and I think this one has been there even in vi for a long time.

Here is a great tip that covers the virtues of :g in vim:

Tip #227: Power of :g

The reason I ended up with this tip was that I needed to find a simple command that would delete all lines matching a pattern.

I knew how to delete all lines that matched a pattern.


:g/pattern/d

but I was looking for the inverse and I found it :g’s cousin :v in the above tip as a user comment.


:v/pattern/d

did the job very well. Once again vim saved the day. 🙂

Advertisements

Written by imsaar

September 25, 2007 at 3:52 pm

Posted in editor

2 Responses

Subscribe to comments with RSS.

  1. Like in “grep” and “grep -v”.

    Thanks for the tip.

    Zac

    January 18, 2008 at 10:12 am

  2. Thanks, that was just what I was looking for. I added it to vimtricks.com

    Poor Yorick

    July 28, 2009 at 4:44 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: