NOTE

VIM regex is based on POSIX: check out the Regex for SED for the core syntax.


VIM-Specific Extensions

VIM introduces a few additions on top of standard POSIX:

PatternMeaning
\dAny digit — shorthand for [[:digit:]]
\wAny word character — shorthand for [[:alnum:]_]
\sAny whitespace — shorthand for [[:space:]]
\uUppercase letter — shorthand for [[:upper:]]
\lLowercase letter — shorthand for [[:lower:]]

Uppercase = opposite, same rule as VSCode: \D, \W, \S match the negation.


Magic Modes

VIM controls how special characters are interpreted via magic modes, set at the start of a pattern:

PrefixModeBehaviour
\mmagic (default)., * are special; +, ?, { need \
\MnomagicOnly ^ and $ are special
\vvery magicAll special chars work without \ — closest to ERE/VSCode
\Vvery nomagicEverything is literal except \

Tip: Use \v at the start of your pattern to write regex in a VSCode/Perl-like style without escaping everything.

Example:

:%s/\v(High|Mid|Low)-level/LEVEL/g

How to Use Regex in VIM

CommandDescription
/patternSearch forward
?patternSearch backward
:%s/pattern/replacement/gReplace all occurrences in the file
:s/pattern/replacement/gReplace on current line only
n / NJump to next / previous match