vim tips: deleting lines matching regex

In vim you can use :global to perform actions based on regular expressions.

Thanks to MichaelRaskin1 in #vim on Freenode I can now remove lines that don’t match what I’m looking for in a file.

To try it out, let’s have a testcase.  You have some truss or strace output you’re looking through and want to see all of the open() calls (without dropping to a shell and running grep); try this:

:%g!/^open/del

This is pretty straightforward: delete (del) all lines (%g) that do not (!) start with (^) open.

Boosh.  Thanks Michael.

Tags: , , , ,

Leave a Reply