Deleting Lines that don't Match a Pattern Wed, 01 May 2013
Something that I found out last night at the Vim London Meetup that was demonstrated by Drew Neil was the ability to delete lines in your file that don’t match a particular pattern. All credit for this tip goes to Drew - I’m just typing it up on here!
Say we have a file that has lines that look a bit like this (I had to do this earlier today):
calculate something_else calculate something_else calculate something_else calculate some_other_thing calculate some_more_things calculate some_other_things something_else something_else
And we want to delete all lines that don’t contain
calculate. We can do that with
:v. Simply running:
Leaves me with:
calculate calculate calculate calculate calculate calculate
This is part of the :global command.
Thanks again to Drew for demonstrating this! If you’re London based, I highly recommend making an effort to attend the meetups. They are completely free and I never come away from one without at least one new trick or tip that improves my usage of Vim.