How do I match a pattern only when it is preceded or followed by another pattern using Vim regex?
\@<= and \@=
Vim's regex engine supports zero-width lookahead and lookbehind assertions using the \@ atom.
\@<= and \@=
Vim's regex engine supports zero-width lookahead and lookbehind assertions using the \@ atom.
:s/pattern/\=expr/
Prefixing the replacement field of :s with \= makes Vim evaluate the rest as a Vimscript expression and use the result as the replacement string.
\@=
Vim's regex engine supports zero-width lookahead and lookbehind assertions using the \@ family of atoms.
:s/\d\+/\=submatch(0)+1/g
The \= prefix in a :substitute replacement field tells Vim to evaluate the following as a Vimscript expression rather than treating it as a literal string.
:'<,'>s/\%Vpattern/replacement/g
When you press : after making a visual selection, Vim inserts ' to restrict the substitution to the selected lines.
visual-mode #visual-mode #search #editing #ex-commands #normal-mode
/\cfoo
Vim's \c and \C flags let you force a search to be case-insensitive or case-sensitive on a per-search basis, regardless of your ignorecase and smartcase setting
:vim /pattern/ **
:vimgrep /pattern/ (shortened to :vim) is Vim's built-in project-wide search.
:%s/\%Vpattern/replacement/g
The \%V atom restricts a regex match to the last visual selection — more precisely than :'s/.
:match {group} /{pattern}/
:match lets you apply a highlight group to any pattern in the current window without touching the buffer or its syntax rules.
<C-q> (Telescope)
While inside any Telescope picker, pressing sends all current results to the quickfix list and closes the picker.
:s/pattern/\=expression/g
Prefixing the replacement string with \= in a :substitute command tells Vim to evaluate the rest as a VimScript expression rather than literal text.
:history
:history displays a numbered list of your recently entered Ex commands, giving you a full audit of what you have run in the current session (and across sessions
:s/,/,\r/g
In Vim's :substitute command, \r in the replacement string inserts a literal newline — it splits the line at that point.
:cdo
:cdo {cmd} executes {cmd} on each entry in the quickfix list — one by one, jumping to each location in turn.
command-line #ex-commands #quickfix #search #editing #buffers
:s/,/\r/g
In Vim's substitute command, use \r (not \n) in the replacement to insert a real newline.
\C
Vim's \C and \c atoms let you override ignorecase and smartcase on a per-pattern basis.
:s/pattern//gn
The :s///gn command counts how many times a pattern appears in the file without actually replacing anything.
command-line #search #ex-commands #substitution #command-line
:let @/ = 'pattern'
Writing to the @/ register via :let @/ = 'pattern' sets Vim's last-search pattern directly — without performing a search or moving the cursor.
:vimgrep /pattern/ **
:vimgrep /pattern/ searches recursively through all files in the current working directory tree using Vim's own regex engine, populating the quickfix list with
\%l and \%c
Vim's \%l and \%c pattern atoms anchor a search to a particular line number or column, enabling surgical searches and substitutions that standard regex cannot e