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

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

  • @0 is the yank register that stores the most recent yank operation
  • @/ is the search register, which controls what n and N repeat
  • \V switches the pattern to very nomagic, so almost every character is treated literally
  • escape(@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 0 when debugging.
  • Pair with :set hlsearch so you can visually confirm the exact literal pattern loaded into @/.

Next

How do I populate a window-local location list from the current file and open it immediately?