Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In vim I need to find all lines that contain one word, but do NOT contain another word and delete them.

I've seen plenty of :g, :g! and :v talk while trying to figure this out but nothing that gives me what I need since it appears I need to combine both :g and :v.

If there's not a way to do it in vim is there another suitably easy way?

share|improve this question

1 Answer 1

up vote 1 down vote accepted
:v/another/s/^.*word.*\n//

should do the trick.

Based on the comment - well, it works for me:

=$ cat sample.txt 
word
another
word another

=$ vim -c ":v/another/s/^.*word.*\n//" -c ":wq" sample.txt 

=$ cat sample.txt 
another
word another

As you can see lines with "word" have been removed, but not if "another" was in the same line.

share|improve this answer
    
didn't work. Appears to have matched the lines containing 'word' and not taken 'another' into consideration at all. –  digitaljoel Feb 13 '13 at 23:51
    
Another is meant to protect line from deletion. Clearly, it works. –  user7385 Feb 14 '13 at 10:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.