Vim: How do I search for a word which is not followed by another word?

preceeded or followed by?

If it's anything starting with 'abc ' that's not (immediately) followed by 'defg', you want bmdhacks' solution.

If it's anything starting with 'abc ' that's not (immediately) preceeded by 'defg', you want a negative lookbehind:

/\%(defg\)\@<!abc /

This will match any occurance of 'abc ' as long as it's not part of 'defgabc '. See :help \@<! for more details.

If you want to match 'abc ' as long as it's not part of 'defg.*abc ', then just add a .*:

/\%(defg.*\)\@<!abc /

Matching 'abc ' only on lines where 'defg' doesn't occur is similar:

/\%(defg.*\)\@<!abc \%(.*defg\)\@!/

Although, if you're just doing this for a substitution, you can make this easier by combining :v// and :s//

:%v/defg/s/abc /<whatever>/g

This will substitute '<whatever>' for 'abc ' on all lines that don't contain 'defg'. See :help :v for more.


Here's the search string.

/abc \(defg\)\@!

The concept you're looking for is called a negative look-ahead assertion. Try this in vim for more info:

:help \@!

Tags:

Vim

Regex