Vim search for a pattern and if NOT occurs delete line

I've read this question, but I want to know how can I delete the line when the pattern DOESN'T occur.

Upvotes: 6

Views: 1083

Answers (3)

Brian Carper
Brian Carper

Reputation: 73006

Equivalently:

:g!/pattern/d

Easier to remember in my opinion, because! is ingrained as "not" in my brain.

Upvotes: 6

DigitalRoss
DigitalRoss

Reputation: 146251

Deleting the rest of the line when a pattern does not occur is ... hard for me to understand.

Did you mean, delete the whole line if it doesn't have a pattern?

:v/pattern/d

If you meant, preserve the line but clobber the characters:

:v/pattern/s/.*//

Upvotes: 17

chaos
chaos

Reputation: 124365

:v/pattern/s/.*//

Upvotes: 1

Related Questions