Vim is looking for a pattern, and if NOT meets the delete string

I read this question, but I want to know how I can delete a row when the pattern does not appear.

+6
vim filter
source share
3 answers

Removing the rest of the line when the pattern does not occur is ... so that I can understand.

Did you mean delete the entire line if it does not have a template?

:v/pattern/d 

If you had in mind, keep the string, but collect the characters:

 :v/pattern/s/.*// 
+17
source share

Equivalent:

 :g!/pattern/d 

It’s easier to remember, in my opinion, because ! rooted as "not" in my brain.

+6
source share
 :v/pattern/s/.*// 
+1
source share

All Articles