How do I change the case of selected text in visual mode?
Answer
U, u, or ~ in visual mode
Explanation
How it works
When you have text selected in visual mode, you can change its case with three simple keys:
U- Convert the entire selection to UPPERCASEu- Convert the entire selection to lowercase~- Toggle the case of each character (upper becomes lower, lower becomes upper)
These commands work in all three visual modes: characterwise (v), linewise (V), and blockwise (Ctrl-V). After the operation, Vim returns to normal mode.
This is particularly useful when you need to fix case inconsistencies, convert constants to uppercase, or transform variable names. You can combine these with text objects for rapid case changes: viwU selects a word and uppercases it, vitU selects inside an HTML tag and uppercases the content.
Note that U and u behave differently in normal mode versus visual mode. In normal mode, u is undo and U undoes all changes on a line. But in visual mode, they control letter case.
Example
Given this text:
hello World
- Press
Vto select the entire line - Press
Uto uppercase everything
Result:
HELLO WORLD
Or use ~ to toggle case:
HELLO wORLD
For a single word, position on it and type viwu to lowercase just that word, or viwU to uppercase it. This is one of the fastest ways to fix capitalization in Vim.