How do I count how many times a pattern appears in a file without making any changes?
:%s/pattern//gn
The n flag on the substitute command reports the number of matches without performing any substitution.
:%s/pattern//gn
The n flag on the substitute command reports the number of matches without performing any substitution.
:keeppatterns %s/old/new/g
The :keeppatterns modifier runs any Ex command without modifying Vim's last search pattern (stored in @/).
command-line #search #ex-commands #command-line #substitute #registers
:s/\v(pattern1)(pattern2)/\2\1/
Vim's substitute command supports capture groups (also called backreferences), which let you rearrange matched portions of text.
:s/^/\=line('.') - line("'<") + 1 . '. '/
When you need to quickly number a set of lines — such as TODO items, steps, or bullet points — you can use a visual selection combined with a substitution e
visual-mode #visual-mode #editing #ex-commands #formatting #substitute
:%s/\<\w\+\>/\=toupper(submatch(0))/g
The \= flag in the replacement part of :substitute tells Vim to evaluate what follows as a Vimscript expression instead of treating it as a literal string.
:&&
After running a :s/pattern/replacement/g command, you often need to repeat it on another line or range.
:cfdo %s/old/new/ge | update
When you grep across your project and want to perform a search-and-replace on every file that matched, :cfdo is the most efficient approach.
command-line #quickfix #substitute #search #ex-commands #editing
:set inccommand=split
Neovim's inccommand option provides real-time visual feedback as you type substitution commands.
:%s/\d\+/\=submatch(0)+1/g
Vim's expression replacement (\=) in substitutions lets you perform arithmetic on matched text.
:%s/\(\w\+\) \(\w\+\)/\2 \1/g
Vim's substitute command supports captured groups (back-references) using \( and \), allowing you to capture parts of a match and rearrange them in the replacem
:%s/\v(old)/\=toupper(submatch(0)[0]).tolower(submatch(0)[1:])/g
Standard substitutions don't preserve the original case of matched text.
:keeppattern %s/old/new/g
When you run a :s or :%s substitute command, Vim updates the search register (@/) with the substitution pattern.
command-line #ex-commands #search #editing #registers #substitute
:%s/\d\+/\=submatch(0)*2/g
Use \= to evaluate expressions.
:%s/<C-r>a/replacement/g
In command-line mode, a inserts the contents of register a.
:%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.
:%s/,/\r/g
In Vim's substitute command, \r in the replacement string inserts a newline.
* 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.
:%s/\<word\>/\u&/g
Vim's substitute command supports case conversion modifiers in the replacement string.