How do I use PCRE-style regex in Vim without escaping every special character?
Answer
\v
Explanation
Vim's default regex mode ("magic") requires backslashes before many special characters: \(, \|, \+, \{. Prefixing your pattern with \v activates very magic mode, where all characters except [0-9A-Za-z_] are treated as regex operators — no extra escaping needed. This makes patterns much more readable, especially if you're accustomed to PCRE or grep ERE syntax.
How it works
\v— enable very magic mode for the rest of the pattern- In very magic mode:
(,|,+,{n},?all work without backslashes - Without it (normal magic): same characters require
\(,\|,\+,\{n\},\?
The switch applies immediately and affects the entire pattern that follows.
Example
Match lines containing either error or warning followed by a colon:
" Normal magic (default) — cluttered with backslashes:
/\(error\|warning\):/
" Very magic — clean and readable:
/\v(error|warning):/
Match a UUID-like pattern:
" Very magic makes grouping and quantifiers readable:
/\v[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
Use in substitutions too:
:%s/\v(\w+)=(\w+)/\2=\1/g
Tips
\V(very NOmagic) does the opposite — almost all characters are treated literally, useful for searching for strings with many special chars like file paths- You can mix modes mid-pattern:
\vand\m(magic),\M(nomagic) are all valid atoms that switch mode at their position - Map a shortcut for faster invocation:
:nnoremap / /\vto always start searches in very magic mode