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

How do I convert text to title case (capitalize first letter, lowercase the rest of each word)?

Answer

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

Explanation

Vim's substitute command supports case-conversion escape sequences in the replacement string. Combining \u (uppercase next character) with \L (lowercase everything that follows) and a captured group lets you convert any mixed-case text to proper title case in a single command.

How it works

  • \(\w\+\) — captures each word (one or more word characters) as group \1
  • \u — uppercases the very next character in the replacement
  • \L — lowercases all subsequent characters until \E or end of replacement
  • \1 — inserts the captured word; \u and \L act on it together

The result: the first character is uppercased, the rest are lowercased — true title case, regardless of the original casing.

Example

Given:

HELLO wORLD, this IS a TEST

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

Hello World, This Is A Test

Tips

  • To apply to only selected lines: :'<,'>s/\(\w\+\)/\u\L\1/g
  • \U (uppercase all) and \L (lowercase all) can be ended with \E:
    • \U\1\E rest — uppercase only the captured group, then continue normally
  • Compare with simpler variants:
    • \u& — capitalizes first character of entire match (does NOT lowercase the rest)
    • \U& — uppercases the entire match
  • For title case that skips short prepositions (a, an, the, of…), a more complex pattern or a \= expression replacement is needed

Next

How do I refer to the matched text in a Vim substitution replacement?