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
pin visual mode: replaces selection → old selection goes into"register (clobbers your yank)Pin 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")
- Yank
fmt.Printlnwithyiw - Visually select
print→ pressP→ register still holdsfmt.Println - Visually select
log→ pressP→ register still holdsfmt.Println - Visually select
echo→ pressP→ 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
viworvitto select precise text objects before pasting - If you accidentally used
pand clobbered the register, remember the original text is now in"and your intended paste is in"1(the numbered register history)