Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 1 down vote accepted

should do the trick.

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

=$ cat sample.txt 
word another

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

=$ cat sample.txt 
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


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.