Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple :g and :v commands in one statement

Tags:

vim

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.

like image 637
Dogbert Avatar asked Aug 04 '12 10:08

Dogbert


1 Answers

I'm no vim wizard, but if all you want to do is "Delete lines which contain foo and do NOT contain bar" then this should do (I tried on your example file):

:v /bar/s/.*foo.*//

EDIT: actually this leaves empty lines behind. You probably want to add an optional newline to that second search pattern.

like image 107
Zecc Avatar answered Sep 20 '22 04:09

Zecc