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

How do I use normal regex syntax in Vim search without escaping everything?

Answer

/\v

Explanation

Vim's default regex syntax requires backslashes before most special characters like +, (, ), {, and |, which is the opposite of what most developers expect from Perl, Python, or JavaScript regex. The \v (very magic) flag switches Vim into a mode where every non-alphanumeric character is treated as special — matching the behavior of modern regex engines.

How it works

  • /\v at the start of a search pattern enables "very magic" mode
  • In very magic mode, +, ?, (, ), {, }, |, and other symbols work as regex metacharacters without backslash escaping
  • Only alphanumeric characters and _ are treated as literal — everything else is a metacharacter
  • This applies to both search (/) and substitute (:s) commands

Comparison

Match a phone number like 555-123-4567:

" Default (magic) mode — painful:
/\d\{3\}-\d\{3\}-\d\{4\}

" Very magic mode — clean:
/\v\d{3}-\d{3}-\d{4}

Capture groups and alternation:

" Default mode:
/\(foo\|bar\)\+

" Very magic mode:
/\v(foo|bar)+

Example

Find all function calls in JavaScript:

/\v\w+\(

Without \v you would need:

/\w\+\(

Use very magic in substitutions too:

:%s/\v(\w+)@(\w+)/\2 [\1]/g

This transforms user@domain into domain [user] using capture groups without backslash clutter.

Tips

  • \V (capital V) is the opposite — "very nomagic" mode where everything is literal except backslash, which is useful for searching literal strings with special characters
  • \m is the default "magic" mode and \M is "nomagic" mode — you rarely need these
  • If you prefer very magic by default, add this mapping to your vimrc: nnoremap / /\v
  • \v works in :g and :v commands too: :g/\v(TODO|FIXME|HACK)/p
  • When using \v, remember that - and . are also special — escape them with \ if you need literals: /\v\d{1,3}\.\d{1,3}
  • The \v flag only affects the pattern after it, so you can mix modes in one pattern if needed (though this is rarely useful)

Next

How do I edit multiple lines at once using multiple cursors in Vim?