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

How do I always access my last yanked text regardless of deletes?

Answer

"0p

Explanation

Register 0 (the yank register) always contains the text from your most recent yank command — and unlike the unnamed register, it is never overwritten by deletes. This solves the classic problem of yanking text, deleting something, and losing your yank.

The problem

yiw     " Yank a word — goes to both "" and "0
dd      " Delete a line — overwrites "" but NOT "0
p       " Pastes the deleted line (from ""), not the yanked word!

The solution

yiw     " Yank a word
dd      " Delete a line
"0p     " Paste from register 0 — always the last YANK

Register behavior summary

Register Contains Updated by
"" (unnamed) Last yank OR delete Both y and d/c/x/s
"0 (yank) Last yank ONLY Only y commands
"1-"9 Delete history Only d/c commands
"- (small delete) Last small delete (<1 line) Small d/c/x

Common workflow

  1. Yank the text you want to paste: yiw
  2. Navigate to target, delete what's there: diw
  3. Paste your yank: "0p

Tips

  • "0 works in insert mode too: <C-r>0 pastes the last yank
  • Many Vim users map p in visual mode to use "0 by default:
    vnoremap p "0p
    
  • Check :reg 0 to see what's in the yank register
  • Understanding registers "", "0, and "1-"9 is crucial for mastering Vim's clipboard system

Next

How do I search across multiple files and navigate results without leaving Vim?