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

How do I search for visually selected text?

Answer

y/<C-r>"<CR>

Explanation

To search for the exact text you have selected in visual mode, yank it and paste it into the search prompt. This is useful when the text contains special characters or spans unusual boundaries.

How it works

  1. Select text in visual mode (v)
  2. Yank with y
  3. Type / to start a search
  4. Press <C-r>" to paste the yanked text into the search prompt
  5. Press <CR> to search

Example

To search for the text obj.method(arg) which contains regex special characters:

  1. Select obj.method(arg) with visual mode
  2. Yank with y
  3. Search with /<C-r>"<CR>

Tips

  • <C-r> followed by a register name pastes that register into the command line
  • Use <C-r><C-w> to insert the word under cursor without yanking
  • Consider escaping the pattern if it contains regex characters
  • Some users map * in visual mode to do this automatically

Next

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