How do I convert a visually selected block of text to uppercase or lowercase?
Answer
U
Explanation
In visual mode, pressing U converts all selected characters to uppercase and u converts them to lowercase. This is faster than using a substitute command and works on any selection — character, line, or block. It is handy for fixing constants, headings, or code identifiers.
How it works
- Enter visual mode with
v,V, or<C-v>and select the text - Press
Uto uppercase oruto lowercase - Vim converts every alphabetic character in the selection and returns to Normal mode
Alternatively, ~ in visual mode toggles the case of each selected character (uppercase becomes lowercase and vice versa).
Example
Convert a constant name to uppercase:
Before: max_retries = 5
^^^^^^^^^^^ (selected with viw)
After: MAX_RETRIES = 5
Convert a heading to lowercase:
Before: # INTRODUCTION
^^^^^^^^^^^^ (selected with v$)
After: # introduction
Tips
gU{motion}— uppercase using a motion without entering visual mode (e.g.,gUiwto uppercase the current word)gu{motion}— lowercase using a motion (e.g.,guiwto lowercase the current word)g~{motion}— toggle case using a motion (e.g.,g~iwto toggle the current word)- These operators are dot-repeatable, making them easy to apply to successive words.