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

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

  1. Enter visual mode with v, V, or <C-v> and select the text
  2. Press U to uppercase or u to lowercase
  3. 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., gUiw to uppercase the current word)
  • gu{motion} — lowercase using a motion (e.g., guiw to lowercase the current word)
  • g~{motion} — toggle case using a motion (e.g., g~iw to toggle the current word)
  • These operators are dot-repeatable, making them easy to apply to successive words.

Next

How do I visually select a double-quoted string including the quotes themselves?