How do I match text only when it is followed by a specific pattern using zero-width lookahead?
\(pattern\)\@=
Vim's regex engine supports zero-width positive lookahead via the \@= operator.
\(pattern\)\@=
Vim's regex engine supports zero-width positive lookahead via the \@= operator.
\_.
In Vim's regular expressions, .
:%s/\d\+/\=printf('%03d', submatch(0))/g
Combining Vim's \= expression substitution with the printf() function lets you apply arbitrary formatting to matched text.
matchadd()
The matchadd() function adds a persistent highlight for a pattern in the current window without touching your search register or interfering with n/N navigation
:%s/\v(\w+)/\u\L\1/g
Vim's substitution engine supports case-modifier sequences in the replacement string, making it possible to convert text to title case in a single command.
search #search #substitution #regex #editing #case #formatting
:'a,'bs/old/new/g
Named marks can serve as range endpoints for any Ex command, including :substitute.
:%s/pattern/replacement/ig
The :s substitute command accepts /i and /I flags that override your global ignorecase and smartcase settings for that single substitution, letting you choose c
:s/\(\w\+\) \(\w\+\)/\=submatch(2) . ' ' . submatch(1)/
The submatch(N) function lets you access individual capture groups inside a \= (expression replacement) in a substitution.
:%s/,/,\r/g
In Vim substitutions, \n and \r behave differently depending on whether they appear in the search pattern or the replacement string — a common gotcha that sur
:%s/\(prefix\)\@<=target/replacement/g
Vim's \@<= is a zero-width look-behind assertion.
\&
Vim's \& operator is the AND combinator in a search pattern.
\zs and \ze in a pattern
Vim's \zs ("match start") and \ze ("match end") atoms let you narrow the actual match region within a broader pattern context.
\C in search pattern
Adding \C anywhere in a search pattern forces case-sensitive matching for that search, overriding the global ignorecase setting.
q?
Vim provides three command-line history windows accessible from normal mode: q: for Ex commands, q/ for forward searches, and q? for backward searches.
\%(...\)
Vim's standard grouping syntax \(.
\n in search, \r in replacement
Vim uses \n and \r differently depending on whether they appear in a search pattern or a replacement string, and mixing them up is a common source of confusion.
searchcount()
The searchcount() function returns a dictionary containing the current search state: which match the cursor is on, how many total matches exist, and whether the
:2match Todo /FIXME/
Vim provides three independent match slots — :match, :2match, and :3match — each of which highlights a pattern using a specified highlight group.
/pattern\c
Vim's \c and \C atoms let you override the global ignorecase and smartcase settings on a per-search basis.
:%s/\v(\w+)/\u\1/g
Vim's substitute command supports case-modifier escapes in the replacement string: \u uppercases the next character, \U uppercases all text until \E or end of r