How do I search for text I just yanked using the register?
Answer
/<C-r>0
Explanation
How it works
After yanking text, you can use it directly as a search pattern by inserting the yank register contents into the search prompt. When you press / to start a forward search, pressing Ctrl-R 0 inserts the contents of the yank register (register 0) at the cursor position on the search line.
This works because Ctrl-R followed by a register identifier inserts that register's contents in any command-line mode, including search mode.
The register options you can use in search:
<C-r>0-- yank register (last yanked text)<C-r>"-- unnamed register (last yank or delete)<C-r>a-- named registera<C-r>=expr-- expression register
After inserting the text, press Enter to execute the search. You can then use n and N to jump between matches as usual.
Example
Suppose you are refactoring and want to find all occurrences of a function name:
- Place your cursor on
calculateTotaland yank the word:yiw - Start a search:
/ - Press
Ctrl-R 0-- the search line now shows/calculateTotal - Press Enter to search
- Use
nto jump to the next occurrence
This is particularly useful for searching for text that contains special regex characters. If you yanked user.name[0], inserting it via Ctrl-R 0 places the literal text in the search field. You may want to prefix the search with \V for very-nomagic mode to treat it as a literal string:
- Type
/\V - Press
Ctrl-R 0 - Press Enter
This ensures that characters like ., [, and ] are not interpreted as regex metacharacters.