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

What are Vim's built-in character class shortcuts for search patterns?

Answer

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

Explanation

How it works

Vim provides shorthand character classes that save you from writing out full bracket expressions. These work in both search patterns and substitute commands:

Class Matches Equivalent
\d digit [0-9]
\D non-digit [^0-9]
\w word character [0-9A-Za-z_]
\W non-word character [^0-9A-Za-z_]
\s whitespace [ \t] (space or tab)
\S non-whitespace [^ \t]
\a alphabetic [A-Za-z]
\l lowercase [a-z]
\u uppercase [A-Z]
\h head of word [A-Za-z_]
\x hex digit [0-9A-Fa-f]

Uppercase versions negate the match (e.g., \D matches anything that is NOT a digit).

Example

Find all lines containing a number:

/\d\+

Find variables that start with an uppercase letter followed by lowercase:

/\u\l\+

Match a phone number pattern like 555-1234:

/\d\{3}-\d\{4}

Find trailing whitespace at the end of lines:

/\s\+$

Extract hex color codes:

/#\x\{6}

These character classes are much more readable than writing out bracket expressions, especially in longer patterns. They also work in substitute commands, for example to remove all digits from a file: :%s/\d//g.

Next

How do you yank a single word into a named register?