Today I Learned

hashrocket A Hashrocket project

Delete Lines with Patterns and Ex Commands

Today I learned some cool ways to delete lines of text in (n)vim. Josh TIL'ed that you can execute an Ex command on a buffer with the :g command. So if you want to delete any lines that match pattern, you can run

:g/pattern/d

Which is pretty neat! But you can also flip it - to delete any lines that don't match the pattern, you can use :v or :g!

:v/pattern/d
:g!/pattern/d

You can also restrict to a range of lines - if you want to only delete lines matching pattern between lines 40 and 50, you can do so with:

:40,50 g/pattern/d
See More #vim TILs
Every developer at Hashrocket is a Vim expert. Check out our development environment, Dotmatrix, and if you are in Chicago, come to the Vim Chicago Meetup hosted at our Chicago office.