Vim Tips Wiki

Switching case of characters

Redirected from VimTip49

1,624pages on
this wiki
Add New Page
Talk0 Share
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.


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 (inner word – cursor anywhere in word).
Toggle case of all characters to end of line.
Toggle case of the current line (same as V~).

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.
First press v or V then move to select text.
If you don't select text, pressing U will undo all changes to the current line.
Change the current line to uppercase (same as VU).
Change current word to uppercase.
Lowercase the visually-selected text.
If you don't select text, pressing u will undo the last change.
Change the current line to lowercase (same as Vu).

Title caseEdit

The :s substitute command can change case (see :help s/\u).

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


Explanation The search pattern is \<\(\w\)\(\w*\)\> which searches for \< (beginning of word), then \w (a word character), then \w* (zero or more word characters), then \> (end of word). The \(...\) create subexpressions to be recalled with \1 and \2 in the replacement. The replacement is \u\1\L\2 which substitutes the two subexpressions transformed: The \u converts the first character of what follows to uppercase, while \L 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 "I'll" or "she's". An alternative based on whitespace for word boundaries is:


Twiddle caseEdit

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')
    let result = toupper(a:str)
  return result
vnoremap ~ y:call setreg('', TwiddleCase(@"), getregtype(''))<CR>gv""Pgv



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


--Jenny 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)

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.