How do I search for the contents of a register without typing the pattern manually?
Answer
:let @/ = @a
Explanation
Vim's search register (@/) holds the current search pattern — the same one used by n, N, *, and hlsearch highlighting. You can write to it directly with :let @/ = @a, making any register's contents the active search pattern. This is especially useful when you've yanked a complex string, a regex, or a word with special characters that would be painful to type into /.
How it works
@/— the search register; setting it is equivalent to running a/pattern<CR>search, but without triggering a cursor jump@a— the contents of named registera(or use@0for the last yank,@"for the unnamed register)- After running the command,
n/Nnavigate matches andhlsearchhighlights them, exactly as after a normal search
Example
You yanked a long function name into register a with "ayiw. Instead of typing:
/myVeryLongFunctionName<CR>
Simply run:
:let @/ = @a
Now n jumps to the next occurrence, N goes backward, and the matches are highlighted — no cursor movement happens on the assignment itself.
Tips
- Use
@0(last yank register) for a quick workflow:yiwthen:let @/ = @0to search for the yanked word - You can set
@/to any Vim regex::let @/ = 'foo\|bar'searches forfooorbar - Combine with
set hlsearch— after assignment, matches light up without a/search - To clear the search highlight without forgetting the pattern, use
:nohl - This is the cleanest way to search for text containing forward slashes, which would otherwise need escaping in
/