Vim Tips Wiki
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 63: Line 63:
 
The following converts the current line to Title Case (all lowercase, except for initial uppercase letters):
 
The following converts the current line to Title Case (all lowercase, except for initial uppercase letters):
 
<pre>
 
<pre>
βˆ’
:s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g
+
:s/\<\(\w\)\(\w*\)\>/\U\1\L\2/g
 
</pre>
 
</pre>
   
βˆ’
'''Explanation''' The search pattern is <code>\<\(\w\)\(\w*\)\></code> which searches for <code>\<</code> (beginning of word), then <code>\w</code> (a word character), then <code>\w*</code> (zero or more word characters), then <code>\></code> (end of word). The <code>\(...\)</code> create subexpressions to be recalled with <code>\1</code> and <code>\2</code> in the replacement. The replacement is <code>\u\1\L\2</code> which substitutes the two subexpressions transformed: The <code>\u</code> converts the first character of what follows to uppercase, while <code>\L</code> converts all of what follows to lowercase.
+
'''Explanation''' The search pattern is <code>\<\(\w\)\(\w*\)\></code> which searches for <code>\<</code> (beginning of word), then <code>\w</code> (a word character), then <code>\w*</code> (zero or more word characters), then <code>\></code> (end of word). The <code>\(...\)</code> create subexpressions to be recalled with <code>\1</code> and <code>\2</code> in the replacement. The replacement is <code>\U\1\L\2</code> which substitutes the two subexpressions transformed: The <code>\U</code> converts the first character of what follows to uppercase, while <code>\L</code> converts all of what follows to lowercase.
   
 
This approach has shortcomings in cases where words may contain what the regular expression recognizes as non-word characters, such as an apostrophe in "<kbd>I'll</kbd>" or "<kbd>she's</kbd>". An alternative based on whitespace for word boundaries is:
 
This approach has shortcomings in cases where words may contain what the regular expression recognizes as non-word characters, such as an apostrophe in "<kbd>I'll</kbd>" or "<kbd>she's</kbd>". An alternative based on whitespace for word boundaries is:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)