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

How do I uppercase only the characters inside a visual selection with :substitute?

Answer

:'<,'>s/\%V./\U&/g

Explanation

When you need to transform text in-place without touching surrounding content, \%V is one of Vim's most precise tools. This command uppercases only characters that fall inside the current visual area, even if your range spans whole lines. It is useful for column-limited edits, partial-line migrations, and refactors where gU would be too broad.

How it works

  • :'<,'> applies the command to the visual line range only (from mark < to >).
  • s/.../.../g runs substitution repeatedly on each line in that range.
  • \%V constrains the pattern to the active visual region, so matches outside the selected columns are ignored.
  • . matches one character at a time within that region.
  • \U& replaces each match with its uppercase form (& is the matched text).

Example

Start with a characterwise visual selection over just foo and bar here:

key=foo status=bar keep=Mixed

Run:

:'<,'>s/\%V./\U&/g

Result:

key=FOO status=BAR keep=Mixed

Tips

  • Swap \U& for \L& to force lowercase in the same selected region.
  • Combine with blockwise visual mode for fixed-column transformations in logs or tables.

Next

How do I launch a GDB session in Vim with the built-in termdebug plugin?