How to Change Case in Vim (to Upper, Lower, etc.)

Change Case in Vim (to Upper, Lower or Title)

by | Aug 12, 2024 | Useful Tech

Want to convert a word to upper case or change just a character? Find out here how to change case in Vim and Neovim (to upper, lower and title case).

First, Negotiating Stairs

The brief fashion of shoes with toes that extended beyond the necessary (like the poulaine) also sparked examples, of course, that took the exercise to ridiculous extremes. Designed to set their wearers apart, these shoes afforded them the fine additional distinction of having to walk up stairs either sideways (like with a pair of skis) or backwards.

Now, while we picture one pair of poulaines on their laborious way up meeting another pair going down the stairs of city hall, let’s turn to elongating characters upwards fast in Vim (or shrinking them back down):

How to Change Case in Vim (to Upper, Lower or Title)

In Normal Mode

Time needed: 1 minute

To change the case of text in Vim or Neovim using normal mode:

  1. Position the cursor on the character, word, line or paragraph you want to change.

  2. Enter normal mode.

    Here’s how: Typically, press Esc to enter normal mode in Vim.

  3. Press G.

    Just one letter: Press ~ (tilde) instead to toggle the case for the current character immediately (from upper to lower or vice versa) and move one character to the right.

  4. Now press the key or key combination for the desired case change.

    Here’s which: In general, use U for lower case, Shift U for upper case and ~ to toggle the current case; see the table below.

  5. Specify the range to which the change should apply.

    Here’s how: All “motion” command in Vim work, e.g., W changes until the end of the current word, A followed by P change the current paragraph and a repletion of the command changes the current line.
    Examples:
    gUaw: changes the current word to upper case
    guu: makes the current line all lowercase
    g~i): toggles case for everything in the closest pair of parentheses

A change gone wrong? How to Undo in Vim

Using Visual Mode

Of course, you can also use visual mode to first make the selection and then apply a case change:

  1. Press V for visual mode, Shift V for visual line mode or Ctrl V for visual block mode.
  2. Use the motion keys to select the portion of text you want to edit.
  3. Press the key or key combination for the desired case change.
    Here’s which: see the table below for an overview.

Vim Keys for Case Toggling (to Upper, to Lower, and Switch)

Vim KeyAction
Uto lower case
Shift Uto upper case
~toggle case
Ztitle case (using the Vim Titlecase plugin)

How to Change Case in Vim (to Upper, Lower, etc.): FAQ

Can I toggle the case in Vim insert mode?

Yes.

While Vim does not come with ready-made commands to change case in insert mode, you can add your own mappings easily.

Example: To set up a Ctrl L to toggle case for the previously typed word, add the following to your .vimrc file:

inoremap <C-L> <Esc>g~iw`]a

Then, for instance, type exit_success and press Ctrl L immediately (and without adding whitespace) to change the whole word to upper case EXIT_SUCCESS.

Can I switch to title case in Vim?

Using search and replace, you can get a basic version of title case in Vim. To change the first letter of each word to upper case with Vim:

  1. Highlight the portion of text you want to change to title case using visual mode or visual line mode.
  2. Press : for the Vim command line.
  3. Now type s/\(w+)/\u\1\L\2/g to replace the first letter with its uppercase equivalent in each word.
    Example: With a selection, the command line will read :'<,’>s/\(w+)/\u\1\L\2/g.
    Headlines: Note that this does not respect headline capitalization conventions.
    Alternative: The Vim Titlecase plugin adds a title case operator to Vim that you can use like the other case keys.
  4. Press Enter.

(How to change case to upper, lower, etc. tested with Vim 9.0–9.1 and Neovim 0.10; first published September 2023, last updated August 2024)

Home » Useful Tech » How to Change Case in Vim (to Upper, Lower, etc.)