How do I search literally for the exact text I yanked last?
Answer
:let @/ = '\V' . escape(@0, '\')
Explanation
When the text you yank contains regex characters, a normal / search can produce noisy or surprising matches. A faster approach is to load the last yank directly into the search register and force literal matching. This gives you an exact, repeatable search target without manual escaping in the command line.
How it works
@0is the yank register that stores the most recent yank operation@/is the search register, which controls whatnandNrepeat\Vswitches the pattern to very nomagic, so almost every character is treated literallyescape(@0, '\\')preserves existing backslashes safely inside the pattern
After running the command once, press n to jump forward through exact matches and N to go backward.
Example
Suppose you yanked this token from a config file:
api.v2/users[active]
Run:
:let @/ = '\V' . escape(@0, '\\')
Now n finds only literal api.v2/users[active] occurrences, instead of treating ., [, and ] as regex operators.
Tips
- This is excellent for logs, URLs, and code symbols with punctuation.
- If your yank includes newlines, Vim still stores it in
@0; inspect with:register 0when debugging. - Pair with
:set hlsearchso you can visually confirm the exact literal pattern loaded into@/.