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

How do I paste over a visual selection without losing my yanked text?

Answer

P (in visual mode)

Explanation

When you paste over a visual selection using p (lowercase), Vim replaces the selection with your register contents — but the replaced text overwrites your unnamed register, making it impossible to paste the same content again. Using P (uppercase) in visual mode pastes from the register just like p, but leaves the register unchanged, so you can continue pasting the same yanked text over multiple selections.

How it works

  • p in visual mode: replaces selection → old selection goes into " register (clobbers your yank)
  • P in visual mode: replaces selection → register is preserved (yank survives)

This distinction matters whenever you need to overwrite several places with the same yanked content without re-yanking between each paste.

Example

You want to replace three words with fmt.Println:

print("a")
log("b")
echo("c")
  1. Yank fmt.Println with yiw
  2. Visually select print → press P → register still holds fmt.Println
  3. Visually select log → press P → register still holds fmt.Println
  4. Visually select echo → press P → all three replaced

With lowercase p, the first paste would clobber your yank with print, making each subsequent paste wrong.

Tips

  • This behavior applies to both character-wise (v) and line-wise (V) visual selections
  • Combine with viw or vit to select precise text objects before pasting
  • If you accidentally used p and clobbered the register, remember the original text is now in " and your intended paste is in "1 (the numbered register history)

Next

How do I run text I've yanked or typed as a Vim macro without recording it first?