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

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 register a
  • <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:

  1. Place your cursor on calculateTotal and yank the word: yiw
  2. Start a search: /
  3. Press Ctrl-R 0 -- the search line now shows /calculateTotal
  4. Press Enter to search
  5. Use n to 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:

  1. Type /\V
  2. Press Ctrl-R 0
  3. Press Enter

This ensures that characters like ., [, and ] are not interpreted as regex metacharacters.

Next

How do you yank a single word into a named register?