How do I uppercase or capitalize matched text inside a Vim substitution?
Answer
:s/\v\w+/\U&/g
Explanation
Vim's substitute command supports special case-conversion sequences in the replacement string, letting you transform matched text to upper or lower case without a separate step. These are especially useful for normalising identifiers, fixing shouting comments, or converting column values in structured text.
How it works
The following escape sequences are available in the replacement portion of :s:
\u— uppercase the next character only\U— uppercase all characters from here until\Eor end of replacement\l— lowercase the next character only\L— lowercase all characters from here until\Eor end of replacement\E— explicitly end a\Uor\Lregion
The special & in the replacement refers to the entire matched text, making it easy to transform without a capture group.
Example
Force every word on the current line to ALL CAPS:
:s/\v\w+/\U&/g
Before: hello world foo
After: HELLO WORLD FOO
Capitalize only the first letter of each word (title case):
:s/\v<(\w)/\u\1/g
Before: the quick brown fox
After: The Quick Brown Fox
Force everything to lowercase:
:s/\v\w+/\L&/g
Tips
- Combine
\uand\Lto produce Title Case while lowercasing the rest of each word:\u\L&. - Apply across a range:
'<,'>s/\v\w+/\L&/glowercases all words in the visual selection. - These sequences are Vim-specific and are not part of POSIX or PCRE regex.