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

How do I replace every character in a visual selection with the same single character?

Answer

r{char} (visual)

Explanation

r{char} in visual mode replaces every character in the selection with the same single character, without entering insert mode. This is faster than using a substitution when you want to overwrite a region with a repeated character — for instance, blanking out sensitive data, filling an area with dashes, or visually aligning content.

How it works

  1. Enter visual mode (v, V, or <C-v>) and select the target region
  2. Press r followed by the replacement character

All selected characters — including whitespace — are replaced with the given character. The cursor stays at the start of the replaced region. Newlines within a linewise or character selection are not replaced; only the characters within the selection are affected.

Example

Given:

Password: s3cr3tP@ss

Position cursor on s, then v$ to select to end of line, then r*:

Password: **********

Or use visual block mode (<C-v>) to replace a column of characters:

Before:
  foo  bar
  baz  qux

After selecting the spaces with <C-v> and pressing r-:
  foo--bar
  baz--qux

Tips

  • Works in characterwise (v), linewise (V), and blockwise (<C-v>) visual modes
  • In linewise mode (V), r{char} replaces every character on every selected line (including leading whitespace) — use with care
  • To replace without affecting whitespace use a substitution instead: :'<,'>s/\S/-/g
  • Contrast with R (replace mode), which lets you type different characters for each position

Next

How do I delete text from the cursor to the next occurrence of a pattern?