How do I change the case of text during a search and replace?
:%s/\<word\>/\u&/g
Vim's substitute command supports case conversion modifiers in the replacement string.
:%s/\<word\>/\u&/g
Vim's substitute command supports case conversion modifiers in the replacement string.
g&
The g& command repeats the last substitute command across the entire file.
/pattern1\_.*pattern2
Vim's \ modifier makes any character class match newlines too.
:%s/\<old\>/new/g
Wrapping your search pattern in \ word boundary anchors ensures that Vim only matches the exact whole word, preventing accidental replacements inside longer wor
:%s/old/new/gc
Adding the c flag to a substitute command makes Vim pause at every match and ask you whether to replace it.
:%s/pattern/\=expression/g
Vim's substitute command supports expression replacements using \= in the replacement string.
/\v
Vim's default regex syntax requires backslashes before most special characters like +, (, ), {, and , which is the opposite of what most developers expect from
:vimgrep /pattern/ **/*.ext | copen
The :vimgrep command searches for a regex pattern across multiple files and populates the quickfix list with every match.
search #search #quickfix #ex-commands #navigation #productivity #grep
/foo\_.*bar
Vim's default .
:args **/*.py | argdo %s/old/new/gc | update
Vim can perform search-and-replace across multiple files without any plugins by combining the arglist with :argdo.
search #search #substitution #ex-commands #productivity #quickfix #arglist
/pattern/+3
Vim's search command accepts an offset after the pattern that shifts where the cursor lands relative to the match.
/\%Vpattern
The \%V atom restricts a search pattern to only match inside the most recent visual selection.
g*
The g command searches forward for the text under the cursor without adding word boundary anchors.
search #search #navigation #normal-mode #motions #productivity
&
The & command in normal mode repeats the last :s substitution on the current line.
search #search #substitution #ex-commands #repeat #normal-mode
:%s//new/g
Leaving the search field empty in a :s command tells Vim to reuse the last search pattern from / or .
search #search #substitution #ex-commands #regex #productivity
gn
The gn motion searches forward for the next match of the last search pattern and visually selects it.
search #navigation #search #motions #normal-mode #repeat #editing
:%s/\(pattern1\)\(pattern2\)/\2\1/g
Vim's substitute command supports capture groups that let you match parts of text, remember them, and rearrange or reuse them in the replacement.
/pattern
The /pattern command searches forward through the file for the given pattern.
#
The # command searches backward for the exact word under the cursor, jumping to the previous occurrence.
*
The command searches forward for the exact word under the cursor, jumping to the next occurrence.