Timothy Grant
Timothy Grant

Reputation: 55

Inverting filter and joining two filters in vim

I am trying to filter a file in gvim using some keywords. The :v command serves the purpose. I do :v/bread/d and I have only the lines containing the word bread. Now, I want to invert the filter to show me only the lines which DO NOT contain the word bread. Can someone please help me with this? And while we are at it, how can we use multiple filters in one go? Right now, I can do :v/butter/d after the previous :v to find only the lines containing both bread(result from previous :v) and butter(result from this :v). But, I would like to combine both bread and butter in one command and possibly, be also able to invert the combination(to show all lines NOT containing bread and butter)

Upvotes: 1

Views: 303

Answers (2)

builder-7000
builder-7000

Reputation: 7627

I would like to combine both bread and butter in one command and possibly, be also able to invert the combination (to show all lines NOT containing bread AND butter)

To show all lines NOT containing bread AND butter:

g/bread.*butter\|butter.*bread/d

Other possible combinations are:

  • To delete all lines containing bread OR butter:

    :g/bread\|butter/d
    
  • To delete all lines NOT containing bread OR butter:

    :v/bread\|butter/d
    

Upvotes: 0

phd
phd

Reputation: 94667

show me only the lines which DO NOT contain the word bread

In your own terms: globally delete lines that contain bread:

:g/bread/d

combine both bread and butter in one command

Remove lines containing neither bread nor butter:

:g!/^.*bread.*$\&^.*butter.*$/d

invert the combination(to show all lines NOT containing bread and butter)

Remove lines containing both bread and butter:

:g/^.*bread.*$\&^.*butter.*$/d

PS. I prefer to write :g! instead of :v but I think these commands are equivalent.

Upvotes: 4

Related Questions