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

How do I replace exactly N consecutive characters with the same character without entering visual mode?

Answer

{count}r{char}

Explanation

The {count}r{char} command replaces a precise number of characters starting at the cursor position with a single repeated character. It is faster than entering visual mode when you know exactly how many characters to overwrite.

How it works

  • {count} — a number specifying how many characters to replace
  • r — the replace operator, which stays in normal mode
  • {char} — the character that replaces each of the counted characters

All {count} characters from the cursor forward are replaced in a single atomic operation, and the cursor lands on the last replaced character.

Example

Given the text with the cursor on the first hyphen:

---> pointer

Pressing 3r= replaces the three characters --> with ===:

===> pointer

Tips

  • Without a count, r{char} replaces only the single character under the cursor — the counted form is the lesser-known extension
  • Useful for replacing separators: 5r- turns five characters into a run of dashes
  • Supports special keys via <C-v>: 3r<C-v><Enter> replaces three characters with literal carriage returns (useful for binary editing)
  • Unlike c or s, r does not leave you in insert mode, so the change is instantly undoable with u

Next

How do I inspect a Vim register's full details including its type (charwise, linewise, or blockwise) in Vimscript?