vimtricks.wiki Concise Vim tricks, one at a time.

What shorthand character classes can I use in Vim regex patterns?

Answer

\w \d \s \a \l \u

Explanation

Vim's regex engine supports a rich set of shorthand character class atoms — backslash sequences that match whole categories of characters. These are more concise than writing out explicit character ranges like [a-zA-Z0-9_] and are the building blocks of practical patterns for searching and substituting.

Core character classes

Atom Matches Inverse (non-matching)
\w word character (letters, digits, _) \W
\d decimal digit [0-9] \D
\s whitespace (space, tab) \S
\a alphabetic character \A
\l lowercase letter \L
\u uppercase letter \U
\p printable character \P
\x hexadecimal digit [0-9a-fA-F] \X

Note: these differ from PCRE/Python regex — \d in Vim only matches ASCII digits, and \w is controlled by the iskeyword setting.

Example

Find all words starting with a capital letter:

/\u\w\+

Match any run of digits:

/\d\+

Replace all whitespace sequences with a single space:

:%s/\s\+/ /g

Tips

  • Use \v (very magic) mode to avoid the backslash-heavy syntax: /\v\d+ instead of /\d\+
  • \k matches a keyword character as defined by iskeyword — useful for matching language-specific identifier characters
  • :help /\w has the complete, authoritative table of all character class atoms
  • Character classes can be combined in brackets too: [\d\s] matches a digit or whitespace

Next

How do I run a search and replace only within a visually selected region?