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

How do I use POSIX character classes in Vim search patterns?

Answer

/[[:digit:][:upper:]]

Explanation

Vim supports POSIX character classes inside bracket expressions, providing a portable and readable way to match categories of characters. These are especially useful when you need to match locale-aware character sets or want more readable patterns than raw ranges.

How it works

  • [:digit:] — digits (0-9)
  • [:upper:] — uppercase letters
  • [:lower:] — lowercase letters
  • [:alpha:] — alphabetic characters
  • [:alnum:] — alphanumeric characters
  • [:space:] — whitespace characters
  • [:punct:] — punctuation characters
  • [:xdigit:] — hexadecimal digits

They must appear inside [] brackets: [[:digit:]]

Example

Find characters that are either digits or uppercase:

/[[:digit:][:upper:]]\+
Text: Hello World 42 test ABC
Matches: H, W, 42, ABC

Tips

  • Combine with negation: [^[:digit:]] matches any non-digit
  • Mix with regular chars: [[:digit:]abc] matches digits or a, b, c
  • [:print:] matches printable characters — useful for finding non-printable bytes
  • Vim also has shorthand equivalents: \d for [:digit:], \a for [:alpha:]

Next

How do I return to normal mode from absolutely any mode in Vim?