vimtricks.wiki Concise Vim tricks, one at a time.

How do I change the case of matched text in a substitute?

Answer

:%s/\(\w\+\)/\u\1/g

Explanation

Vim provides case-conversion atoms in substitute replacements: \u uppercases the next character, \l lowercases it, \U uppercases until \e, and \L lowercases until \e.

How it works

  • \u — uppercase the next character
  • \l — lowercase the next character
  • \U — uppercase all following characters until \e
  • \L — lowercase all following characters until \e
  • \e — end case conversion

Example

Capitalize first letter of every word:

:%s/\(\w\+\)/\u\1/g

Before: hello world → After: Hello World

Convert to uppercase:

:%s/error/\U&/g

Before: error → After: ERROR

Tips

  • & refers to the whole match
  • \u\U combines: first character behavior then rest
  • These only work in the replacement part of :s
  • Combine with capture groups for precise transformations

Next

How do you yank a single word into a named register?