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

How do I highlight all occurrences of a yanked word without typing a search pattern?

Answer

:let @/ = @"

Explanation

After yanking text, you can promote it directly to the search register with :let @/ = @". This highlights every match via hlsearch and lets you jump between them with n/N — without ever opening a / search prompt or retyping the text.

How it works

  • @" — the unnamed register, which holds the most recently yanked (or deleted) text
  • @/ — the search register, which stores the current search pattern and drives hlsearch highlighting and n/N navigation
  • :let @/ = @" — assigns the yanked text to the search register, activating all search machinery immediately

Example

You want to find all occurrences of a long variable name without retyping it:

Step 1: Position cursor on the name and run  yiw  (yank inner word)
Step 2: :let @/ = @"   ← press Enter
Step 3: All matches light up; use n / N to hop between them

For multi-word selections, yank in visual mode first:

viwy          " visually select and yank
:let @/ = @"  " activate as search pattern

Tips

  • Combine with :set hlsearch if highlighting isn't on by default
  • Unlike * (which only matches whole words), this approach respects the exact text you yanked — including spaces, symbols, and partial words
  • You can also use named registers: :let @/ = @a promotes register a to the search pattern
  • To clear the highlight afterward, run :noh

Next

How do I toggle a fold and all its nested folds open or closed at once?