Another old post from the “Unseen Alan” files. I wrote it after downloading a file full of UPPERCASE BABY NAMES. I can’t recall why I hadn’t posted this one.
Originally written November 11, 2003.
To put a file into title case:
First, make every character lower-case. ”\L” in the substitution pattern makes all following letters lower-case.
%s/./\L&/g
Second, change the case of the first letter in each word. ”\w” in the search pattern finds word characters. ”\u” in the substitution pattern makes the single next character upper-case.
%s/\w*/\u&/g
Comments
There's no need for two steps, just use subexpressions.
\< and \> are start and end of word (you could leave them off in this case, because greedy matching will match the whole word; it's still good defensive regexp coding practice). \( \) wrap each subexpression so it can be recalled later with \1..\9.