Title Case With Vim

11 AM April 29, 2004

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

By alang | # | Comments (1)
(Posted to Software Development)

Comments

At 01:30, 30 Apr 2004 Mark Hughes wrote:

There's no need for two steps, just use subexpressions.

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

\< 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.

(#)

Add Comment




(Not displayed)






(Leave blank line between paragraphs. URLs converted to links. HTML stripped. Indented source code will be formatted with <pre> tags.)




© 2003-2006 Alan Green