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

How do I title-case every word in a buffer using one Vim substitution?

Answer

:%s/\v<(\w)(\w*)>/\u\1\L\2/g

Explanation

When you need to normalize casing across headings, labels, or generated docs, editing words one by one is tedious. This substitution converts each word to title case in a single pass: first letter uppercase, remainder lowercase. It is a practical cleanup step after importing noisy text from external tools.

How it works

:%s/\v<(\w)(\w*)>/\u\1\L\2/g
  • :%s targets the whole buffer
  • \v enables very-magic regex syntax
  • <(\w)(\w*)> matches each word boundary and splits the word into first character plus remainder
  • \u\1 uppercases capture group 1 (the first letter)
  • \L\2 lowercases capture group 2 (the rest of the word)
  • g applies this to every word occurrence on each line

Example

Before:

ERROR LEVEL summary
second LINE OF TEXT

After:

Error Level Summary
Second Line Of Text

Tips

  • To avoid touching short acronyms, run with c and confirm selectively
  • Restrict the range (for example :10,40s/...) when you only want part of a file normalized
  • Combine with marks so you can jump back to your original editing location quickly

Next

How do I regenerate help tags for every installed plugin in one command?