FANDOM


(Examples)
(/Title case /)
Line 44: Line 44:
   
 
==Title case==
 
==Title case==
The <code>:s</code> substitute command can change case (see {{help|s/\u}}).
+
The</code> substitute command can change case
   
 
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>
+
'''Explanation''' The search pattern is <code>which searches for <code> then <code></code> (a word character), then <code>\</code> (zero or more word characters), then <code></code> (end of word). The <code></code> create subexpressions to be recalled with <code> and <code>in the replacement. The replacement is <code><code> which substitutes the two subexpressions transformed: The <code></code> converts the first character of what follows to uppercase, while <code></code> converts all of what follows to lowercase.
:s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g
 
</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.
 
   
 
==Twiddle case==
 
==Twiddle case==

Revision as of 15:34, March 8, 2013

Tip 49 Printable Monobook Previous Next

created 2001 · complexity basic · version 5.7


You can change the case of text:

Toggle case "HellO" to "hELLo" with g~ then a movement.
Uppercase "HellO" to "HELLO" with gU then a movement.
Lowercase "HellO" to "hello" with gu then a movement.

Alternatively, you can visually select text then press ~ to toggle case, or U to convert to uppercase, or u to convert to lowercase.

Examples

Toggle case of the character under the cursor, or all visually-selected characters.
Toggle case of the next three characters.
Toggle case of the next three words.
Toggle case of the current word
Toggle case of all characters to end of line.
Toggle case of the current line

The above uses ~ to toggle case. In each example, you can replace ~ with u to convert to lowercase, or with U to convert to uppercase. For example:

Uppercase the visually-selected text.
Change the current line to uppercase
Change current word to uppercase.
Lowercase the visually-selected text.

Title case

The</code> substitute command can change case

The following converts the current line to Title Case (all lowercase, except for initial uppercase letters):

Explanation The search pattern is which searches for <code> then <code> (a word character), then \ (zero or more word characters), then (end of word). The create subexpressions to be recalled with and <code>in the replacement. The replacement is <code><code> which substitutes the two subexpressions transformed: The <code> converts the first character of what follows to uppercase, while converts all of what follows to lowercase.

Twiddle case

With the following (for example, in vimrc), you can visually select text then press ~ to convert the text to UPPER CASE, then to lower case, then to Title Case. Keep pressing ~ until you get the case you want.

function! TwiddleCase(str)
  if a:str ==# toupper(a:str)
    let result = tolower(a:str)
  elseif a:str ==# tolower(a:str)
    let result = substitute(a:str,'\(\<\w\+\>\)', '\u\1', 'g')
  else
    let result = toupper(a:str)
  endif
  return result
endfunction
vnoremap ~ ygv"=TwiddleCase(@")<CR>Pgv

References

Comments

The following will skip single-letter words and words that aren't in uppercase. It also accounts for non-english latin characters.

:s/\v\C<([A-ZÀ-Ý])([A-ZÀ-Ý]+)>/\u\1\L\2/g

--Jenny 165.2.186.10 19:05, April 5, 2012 (UTC)

Nice, thanks. I added \C to your command above to make the search case sensitive (it won't skip lowercase words if 'ignorecase' is set, unless \C is present). JohnBeckett 09:58, April 6, 2012 (UTC)
Community content is available under CC-BY-SA unless otherwise noted.