I expect this to select all non-alphanumeric characters:


Instead it selects all non-w's.

This is the regex I would use in JavaScript, Python etc.:


How can I write this so that it's compatible with vim?

  • Hang on, don't you want simply \W? Edit: Oh, nevermind, you could not use that with other classes in a bracket expression.
    – Quasímodo
    Nov 2 '20 at 12:29
  • 1
    @Quasímodo yeah it would be a little verbose (\W\|\S\|[^"])
    – D. Ben Knoble
    Nov 2 '20 at 14:17

The [..] form is known as a collection (:h /[]) and can contain a sequence of characters and/or one or more ranges of characters, separated with -. It will match any single character that is contained in the sequence/range(s). You can, of course, also match the inverse with ^.

In :h white-space you'll find a collection equivalent to \w:

\w  word character:         [0-9A-Za-z_]

So one approach is to invert/negate that equivalent:


Alternatively, collections can also contain the "character classes" found under :h E944. These are the same character classes that have been around forever in the BRE/ERE patterns used by legacy tools and POSIX environments.

Alphanumeric chars have the following character class equivalent:

[:alnum:]   isalnum ASCII letters and digits

You can invert/negate that like so:


(You could also probably use negative lookarounds with \w but that's relatively difficult compared to this straightforward analog to [^\w].)

Update: I just realized I didn't address the full pattern you posted. Just look for a character class that's equivalent to \s and you'll find [:space:]. And " is just ". So [^\w\s"] becomes:


One other thing, this is likely to be the most performant of any possible solutions since others have lookarounds or alternations (|) and those are expensive.

  • Is [:alnum:] etc NeoVim only as in vim 8.0.586 /[:alnum:] highlights some alphabetic c characters, not all , and no digits /[:digit:] highlights different alphabetic characters and no numeric characters at all
    – Steve
    Nov 2 '20 at 23:01
  • [:alnum:] will highlight a, l, n, u, m and :. You want [[:alnum:]]. Character classes must be in a collection.
    – B Layer
    Nov 3 '20 at 0:15
  • @B Layer Thanks. That helps a lot.
    – Steve
    Nov 3 '20 at 1:37

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.