Vim regex: very magic, \zs and expressions

Vim · Expert · 15 min

Vim's regex dialect differs from PCRE; knowing its tools turns hard refactors into one substitute.

Vim's default magic mode treats . * [] ^ $ as special but needs backslashes for \+, \?, \|, \(\) and \{n,m}. Starting a pattern with \v (very magic) makes every punctuation character special, closer to PCRE: /\v(foo|bar)+ instead of /\(foo\|bar\)\+. \V (very nomagic) does the opposite, so only \ is special, useful for literal text.

Vim-specific atoms:

  • \< \> word boundaries (in \v mode just < >).
  • \zs and \ze set where the match starts and ends while still requiring the context: /foo\zsbar matches bar only after foo. This replaces most lookbehind/lookahead needs.
  • Real lookarounds exist too: \(foo\)\@<=bar, foo\(bar\)\@!.
  • \{-} is the non-greedy quantifier (.\{-} vs .*); in very magic {-}.
  • \_s and \n match across line breaks; \_. is any character including newline.
  • \c / \C anywhere in the pattern force case-insensitive / sensitive.

In a replacement, \= evaluates an expression, with submatch(n) for groups: :%s/\d\+/\=submatch(0)*2/g doubles every number. \u/\U/\L change case of what follows. These combine with :g for surgical refactors.

Example

/\v(get|set)_\w+            " very magic: no backslash soup
/\Vfile.txt                  " literal text: '.' is just a dot
:%s/\vuser_\zsid/ident/g    " rename only the 'id' part of user_id
:%s/".\{-}"/"X"/g          " non-greedy: "hi" and "yo" -> "X" and "X"
:%s/\d\+/\=submatch(0) * 2/g  " double every number
:%s/\v<(\w)(\w*)>/\u\1\L\2/g  " Title Case every word
:%s/,\n\s*/, /             " join continuation lines

Remember

  • \v very magic = PCRE-like, \V = literal
  • \zs/\ze trim the match but keep the context
  • \{-} is non-greedy; \= computes the replacement

Watch out for

  • Pasting a PCRE pattern like (a|b)+ into a default-magic search; without \v it looks for literal parentheses.

Check yourself

  1. Which pattern matches `bar` only when it directly follows `foo`, highlighting just `bar`?
  2. What does `.\{-}` mean in Vim?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: vimhelp.org/pattern.txt.html#%2F%5Cv

Builds on

Unlocks

Nothing builds on this yet.