Is there a command in Vim that changes the case of the selected text?
Asked
Active
Viewed 2.5e+01k times
584
-
5A helpful link I guess: http://vim.wikia.com/wiki/Switching_case_of_characters – yuan Nov 02 '13 at 14:18
3 Answers
779
Visual select the text, then U for uppercase or u for lowercase. To swap all casing in a visual selection, press ~ (tilde).
Without using a visual selection, gU<motion>
will make the characters in motion
uppercase, or use gu<motion>
for lowercase.
For more of these, see Section 3 in Vim's change.txt help file.

Community
- 1
- 1

Mark Rushakoff
- 249,864
- 45
- 407
- 398
-
9In experimenting, it looks like `g~
` works, too. May want to add that, I tend to use `~` exclusively. – trysis Jul 21 '19 at 14:41 -
29Which means that we can use `gUiw` to turn a word into uppercase. Thanks! – lucidbrot Aug 24 '19 at 11:51
-
4
-
2@Ilias Karim, not exactly the same! Without the `i` (for inner) you only change from the cursor position to the end of the word, if you use `gUw`. – jens.klose May 07 '23 at 10:19
519
See the following methods:
~ : Changes the case of current character
guu : Change current line from upper to lower.
gUU : Change current LINE from lower to upper.
guw : Change to end of current WORD from upper to lower.
guaw : Change all of current WORD to lower.
gUw : Change to end of current WORD from lower to upper.
gUaw : Change all of current WORD to upper.
g~~ : Invert case to entire line
g~w : Invert case to current WORD
guG : Change to lowercase until the end of document.
gU) : Change until end of sentence to upper case
gu} : Change to end of paragraph to lower case
gU5j : Change 5 lines below to upper case
gu3k : Change 3 lines above to lower case

Kes
- 285
- 2
- 17

ungalnanban
- 9,539
- 10
- 44
- 55
-
1In this case, the aw and iw commands would do the same thing since whitespace doesn't have a case. I believe we can save a keystroke and go with the w versions of the command. Is there any reason to use aw here? – batbrat Apr 05 '19 at 16:48
-
17`guw` changes the case from the current position until the end of the word. `guaw` or `guiw` changes the case of the whole word. – Victor Schröder May 10 '19 at 16:17
3
Additionally, although all is said and its not for visual selection:
There are operators:
Usage: operator motion
See :h operator
and :h motion
Operators can be
c change
d delete
gu make lowercase
gU make uppercase
...
The motions are mostly well known:
0 first character of the line
$ end of line
aw a word
iw inner word
...
So you have to remember just a few operators and the motions (there are much but you will have some favorites).
In this way you get the list of @ungalnanban above.
Found on Vim cheatsheet - devhints.io

Andy A.
- 1,392
- 5
- 15
- 28