How do I search and replace text containing special characters?
:%s/\V literal.text/replacement/g
The \V (very nomagic) flag treats all characters as literal except for \.
:%s/\V literal.text/replacement/g
The \V (very nomagic) flag treats all characters as literal except for \.
:cdo s/old/new/g
The :cdo command executes a command on every entry in the quickfix list.
:set incsearch
The incsearch option enables incremental search, which highlights matches in real time as you type the search pattern.
/\%>5c\%<20cpattern
How it works Vim's search patterns support column position constraints using the \%c family of atoms.
\zs and \ze
How it works Vim's \zs (set start) and \ze (set end) atoms let you define which portion of a pattern counts as the actual match, even though the full pattern is
:g/pattern/t$
How it works The :g (global) command combined with :t (copy) lets you duplicate all lines matching a pattern to a specific location.
[I
How it works The [I command searches the current file (and included files) for the word under the cursor and displays a list of all matching lines with their li
\d \w \s \a \l \u
How it works Vim provides shorthand character classes that save you from writing out full bracket expressions.
.\{-}
How it works In most regex engines, ? or +? make quantifiers non-greedy (matching as little as possible).
/foo\|bar
How it works The \ operator in Vim's search pattern works like a logical OR, letting you match any one of several alternatives.
q/
How it works Vim keeps a history of all your search patterns.
/\cpattern
How it works By default, Vim searches are case-sensitive: /Hello will not match hello or HELLO.
:%s/pattern/\=MyFunc(submatch(0))/g
The \= prefix in Vim's substitute replacement invokes the expression register, which can call any Vimscript function.
:%s/pattern/\=@0/g
The \=@0 replacement expression inserts the contents of register 0 (last yank) as the replacement text.
/\%>10l\%<20lpattern
Vim's \%>Nl and \%10l matches only after line 10 \%10l\%10l\%<20lold/new/g Combine with column restrictions for precise region targeting Tips Line numbers in \%
/\(pattern\)\@<=target or /target\(pattern\)\@=
Vim supports zero-width assertions (lookahead and lookbehind) in its regex engine.
:set incsearch hlsearch
The combination of incsearch and hlsearch gives you live, interactive search highlighting.
:vimgrep /pattern/g **/*.ext
The :vimgrep command searches for a pattern across multiple files and loads the results into the quickfix list.
* then :%s//new/g
Pressing searches for the word under the cursor, which also loads it into the search register.
:'<,'>s/\%Vold/new/g
The \%V atom restricts a search pattern to match only within the visual selection area, including visual block selections.