Multiple :g and :v commands in one statement

Viewed 1775

I have this file

foo
foo bar
foo bar baz
bar baz
foo baz
baz bar
bar
baz
foo 42
foo bar 42 baz
baz 42

I want to

  1. Select lines which contain foo and do NOT contain bar
  2. Delete lines which contain foo and do NOT contain bar

I read somewhere (can't find the link) that I have to use :exec with | for this.

I tried the following, but it doesn't work

:exec "g/foo" # works
:exec "g/foo" | exec "g/bar" -- first returns lines with foo, then with bar
:exec "g/foo" | :g/bar -- same as above

And ofcourse if I cannot select a line, I cannot execute normal dd on it.

Any ideas?

Edit

Note for the bounty:

I'm looking for a solution that uses proper :g and :v commands, and does not use regex hacks, as the conditions may not be the same (I can have 2 includes, 3 excludes).

Also note that the last 2 examples of things that don't work, they do work for just deleting the lines, but they return incorrect information when I run them without deleting (ie, viewing the selected lines) and they behave as mentioned above.

11 Answers
Related