How do I change the case of matched text in a Vim substitute command?
\u and \l in :s replacement
Vim's substitute command supports special case modifiers in the replacement string that let you change the case of captured text on the fly.
139 results for "S substitute line"
\u and \l in :s replacement
Vim's substitute command supports special case modifiers in the replacement string that let you change the case of captured text on the fly.
:%s/pattern/\=expression/g
Vim's substitute command supports expression replacements using \= in the replacement string.
:s/\v\d+/\=printf('%04d', submatch(0))/g
Substitution expressions let Vim compute each replacement dynamically, which is ideal when plain capture groups are too limited.
&
The & command in normal mode repeats the last :s substitution on the current line.
search #search #substitution #ex-commands #repeat #normal-mode
:%s/\d\+/\=printf('%04d', submatch(0))/g
When you need fixed-width numeric fields, manually editing each number is slow and error-prone.
:s/,/\r/g
In Vim's substitute command, use \r (not \n) in the replacement to insert a real newline.
:%s/\v^([^,]+),([^,]+),/\2,\1,/<CR>
When CSV-like data has two columns in the wrong order, manually fixing each line is slow and error-prone.
:'<,'>s/\%V./\U&/g
When you need to transform text in-place without touching surrounding content, \%V is one of Vim's most precise tools.
:%s/\V<C-r>//gc
When your last search pattern contains punctuation, slashes, or regex atoms, retyping it inside :substitute is error-prone.
:%s/\vfoo\zsbar/baz/g
When your match has a stable prefix but you only want to replace the trailing segment, \zs is often cleaner than introducing extra capture groups.
:keeppattern s/old/new/g
When you run a :s or :g command, Vim updates the search register (@/) with the pattern you used.
:keepmarks
When Vim executes commands that move or reorder lines — such as :sort, :%!sort, or :s/// across ranges — it automatically adjusts named marks to follow the
:%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.
:%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
:%s/old/new/g
The :%s/old/new/g command replaces all occurrences of old with new across every line in the file.
:%s/pattern//n
The n flag in the substitute command suppresses the actual replacement and instead reports the match count.
:s/\v(pattern1)(pattern2)/\2\1/
Vim's substitute command supports capture groups (also called backreferences), which let you rearrange matched portions of text.
g&
The g& command repeats the last substitute command across the entire file.
\%V
The \%V atom in a Vim pattern matches only inside the last visual selection.
search #search #visual-mode #substitute #regex #text-objects
:%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