I need to search for lines which contain some word but do not contain a second word.

For example, find lines that contain the word "This" but not the word "red"

so that for a file containing these lines

This is a blue coat
This is a red  coat
This is a purple coat
That is a coat

The search would locate the first and third lines only.

Thanks for any advice!

  • You can use the LogiPat plugin that is distributed with newer Vims – Christian Brabandt Sep 29 '15 at 6:02

You can use this command to print lines containing This and not red:

  • \& separates the branches like a logical AND because we want the lines that satisfy the two conditions (branches),
  • \(.*This\) is the first branch, it could simply be This but it is more generic that way,
  • \(.*red\)\@! is the second branch, \@! means "zero width match if the preceding atom doesn't match", like a logical NOT.

In other words:

\(.*This\)  \&  \(.*red\)\@!
°°°°°°°°°°  °°  °°°°°°°°°°°°
'This'      AND NOT 'red'
  • Much thanks, this is very much like a query through formatted text. I was able to use this to find functions missing docstrings. – Joshua Detwiler Jul 23 '18 at 17:14

Take a look at this post.

Combine it with @romainl answer and you can do whatever you want.

Example: Let's say I want to add a const to the functions which doesn't have them.

    handleChange = value => {

VIM script:

%s/\([ ]*\)\(\w* = .*$\)\&^\(\&\(.*const.*\)\@!.\)*$/\1const \2/gc


    const handleChange = value => {


\([ ]*\)\(\w* = .*$\)

- get me all lines which has word =


- combine rules


- the line shouldn't contain const.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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