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

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

Answer

Vr=

Explanation

In visual mode, pressing r followed by a character replaces every character in the selection with that single character. This is a fast way to draw ASCII dividers, redact sensitive content, or fill a fixed-width region.

How it works

  • v (or V for linewise, <C-v> for block) enters visual mode and you select text
  • r is the replace operator — in visual mode it applies to every character in the selection
  • {char} is the replacement character (e.g., -, =, *, )
  • Newlines within a linewise selection are preserved; only printable characters are replaced

Example

Given:

Section Title

Place the cursor on S, press V to select the line, then r= to get:

=============

Or select a word with viw then press r* to redact:

before: my secret password here
after:  my ****** ******** here

Tips

  • <C-v>jr- on two lines creates a two-row block of dashes — useful for ASCII table borders
  • Combine with a count: 5r= in normal mode replaces the next 5 characters, but in visual mode the selection determines the span
  • To blank out a region instead of deleting it (preserving column alignment), select with <C-v> and press r<Space>

Next

How do I refer to the matched text in a Vim substitution replacement?