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

How do I overtype text in Vim without breaking column alignment when tabs are involved?

Answer

gR

Explanation

gR enters Virtual Replace mode, a smarter variant of Replace mode (R) that replaces characters based on screen columns rather than raw bytes. This means that overtyping a tab character behaves as if you are replacing its visual width — each new character you type consumes one visual column of the tab rather than overwriting the entire tab at once.

How it works

  • R — classic Replace mode: replaces raw bytes one-for-one. Typing over a tab replaces the whole tab with a single character, destroying your alignment.
  • gR — Virtual Replace mode: replaces by screen position. Typing over a tab consumes it one visual column at a time, keeping the text to the right of the cursor pinned in place.

This makes gR ideal for editing fixed-width text, aligned data, or any file where you need column alignment to stay intact.

Example

Given a line where name is padded with a tab to column 16:

first_name      Alice

In R mode, typing over the tab immediately collapses the spacing. In gR mode, you can safely overtype characters in the first_name field without disturbing Alice's column position.

Tips

  • gr (lowercase) is the single-character variant: it replaces exactly one character by virtual column, then returns to Normal mode — analogous to r vs R.
  • Both gR and gr respect the 'virtualedit' setting and multibyte characters.
  • Press <Esc> to exit Virtual Replace mode and return to Normal mode, just like any other insert-family mode.

Next

How do I programmatically set a register's content in Vimscript to pre-load a macro?