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

I know how to do something to every line that matches a pattern, using :g. Is there a way to do something to every line that does not match the pattern?

For example, suppose I want to delete every line that does not match /foo/. I might do something like:


(:G is not the opposite of :g, but that is what I am looking for)

Any ideas?

share|improve this question
miv ni g: . – tsilb Aug 31 '09 at 3:46
up vote 16 down vote accepted

I think there is a inversion within VIM as,


or, like the grep command (below),


If you are ready to go to the shell prompt,

grep -v foo filename.txt > new.txt

will do this

share|improve this answer
That did it, thanks! – pkaeding Aug 31 '09 at 3:19

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.