vimtricks.wiki Concise Vim tricks, one at a time.

How do I convert the entire current line to lowercase or uppercase in one command?

Answer

guu / gUU

Explanation

Vim's case operators gu (lowercase) and gU (uppercase) follow the same doubling convention as dd and yy: repeating the operator letter applies it to the whole current line. guu lowercases the line; gUU uppercases it — no motion or text-object needed.

How it works

  • guu — convert the current line to all lowercase
  • gUU — convert the current line to all UPPERCASE
  • g~~toggle the case of every character on the current line
  • These are shorthand forms; gul / gUl + $ would achieve the same result but are more verbose
  • A count prefix affects multiple lines: 3guu lowercases 3 lines starting from the current

Example

Before:

Const API_BASE_URL = 'https://Api.Example.Com';

After guu:

const api_base_url = 'https://api.example.com';

After gUU (from the original):

CONST API_BASE_URL = 'HTTPS://API.EXAMPLE.COM';

Tips

  • For finer granularity: guiw lowercases the current word, gUiw uppercases it
  • guu is especially handy when normalizing ALL-CAPS constants or accidentally-shifted lines
  • In Visual mode, select a range of lines and press u (lowercase) or U (uppercase) to apply case conversion to the entire selection
  • The case operators respect multibyte characters in most cases, though behavior depends on the locale and Vim's encoding setting

Next

How do I run a search and replace only within a visually selected region?