How do I use case modifiers like \u and \U in a substitution replacement to change capitalization?
Answer
:%s/\v(\w+)/\u\1/g
Explanation
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 replacement, \l lowercases the next character, and \L lowercases all text until \E. These let you transform capitalization directly inside a :s command without any external tools.
How it works
| Modifier | Effect |
|---|---|
\u |
Make the next character uppercase |
\U |
Make all following characters uppercase until \E |
\l |
Make the next character lowercase |
\L |
Make all following characters lowercase until \E |
\e / \E |
End the effect of \U or \L |
These modifiers apply to both literal text and backreferences like \1.
Example
Title-case every word on every line (capitalize first letter of each word):
:%s/\v(\w+)/\u\1/g
Before:
hello world test
After:
Hello World Test
Convert uppercase identifiers to lowercase:
:%s/\v([A-Z]+)/\L\1\E/g
Tips
\u\1is the most common pattern: capitalize the first letter of a captured group- Combine with very magic mode (
\v) for cleaner capture group syntax:\v(\w+)instead of\(\w\+\) - Use
\L...\Eto lowercase a whole backreference::%s/\v(ERROR)/\L\1\E/g→error - These modifiers work only in the replacement side of
:s, not in the search pattern itself