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

How do I search for the exact text I have selected?

Answer

y/\V<C-r>"<CR>

Explanation

By yanking a visual selection and pasting it into the search prompt with \V (very nomagic), you can search for exact text including special characters.

How it works

  1. Select text in visual mode
  2. y yanks the selection
  3. /\V starts a literal search
  4. <C-r>" pastes the default register into the search prompt
  5. <CR> executes the search

Example

To search for the exact text obj.method(x) which contains regex special characters, select it visually, then y/\V<C-r>"<CR>.

Tips

  • \V prevents . and () from being interpreted as regex
  • <C-r> followed by a register name pastes it into the command line
  • This is useful for text that contains regex metacharacters
  • Some users create a mapping for this common operation

Next

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