Saari Development

This blog is intended to be a log of my (Ali Rizvi's) professional ramblings as a software development engineer. I intend to add logs of my experience with different technologies, software, tech books/articles and related stuff from time to time. My intention is to have an archive for my personal use and public benefit.

Tuesday, September 25, 2007

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

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.


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


did the job very well. Once again vim saved the day. :-)


Blogger rovertus said...

:g!/pattern/d will also do the inverse!

8:49 AM  
Blogger Ben Oakes said...

That's awesome -- thanks for sharing. I almost always use grep for stuff like that.

10:10 AM  
Blogger Bitchslap said...

Awesome tip, thanks for posting!

4:38 PM  
Blogger David said...

Thanks for the tip, very useful.

1:21 AM  

Post a Comment

<< Home