I've the following content:

a1a foo bar buzz
foo b2b bar buzz
foo bar c3c buzz
foo bar buzz d4d

where I can match all words consisting only alpha characters by the following pattern:


Now, how can I remove all words which doesn't match the pattern?

So the final content is:

foo bar buzz
foo bar buzz
foo bar buzz
foo bar buzz

I've checked Search for lines not containing pattern page, but without luck.

To remove some words patching the pattern, global command can be used:


as per this post.

However when doing opposite:


there is this message:

Pattern found in every line

  • In this case, you could simply search for \a\d\a and remove that. But that doesn't answer the general question of How to remove something that doesn't match a search
    – DJMcMayhem
    Oct 5, 2017 at 17:10

1 Answer 1


You can do this with \@<! (which is sometimes called "negative lookbehind"):


    \@<! Matches with zero width if the preceding atom does NOT match just
         before what follows.

It is a bit tricky to understand at first:

  1. Initially we try searching for all words (including ones with numbers) /\<\w\+\>.
  2. Since we want some words to match and not others, we use a "zero-width" in this position to force certain text to match. In this case, the text we want is words without a digit: \<\a\+\>.
  3. We wrap the second pattern in \(...\)\@<!, which denotes the zero-width mentioned above. We use \(...\) to create a single atom.

Now, to remove those words, we can use :substitute, but add \s\? to take care of the spaces.


We can make this slightly shorter using \v very magic:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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