I want to yank text from a window and search for it but the text contains characters not interpreted literally by the search command, e.g. a file path. Is there an easy way I can quote the yanked text when entering the search command?
You can search for literal text by using the
\V modifier, which means very nomagic after which only the backslash keeps it special meaning.
In my .vimrc I have this command to search for text literally:
:com! -nargs=1 Search :let @/='\V'.escape(<q-args>, '\\')| norm! n
There is unfortunately no option to have search input interpreted as raw text.
There is something close to it though: the
nomagic option causes most characters (but not all) to be interpreted literally.
nomagic for a single search, prefix the search pattern with
You can also activate
nomagic globally (
:set nomagic). Vim's help warns that this might break plugins (though many plugin writers take care to explicitly set the desired magic mode for their patterns).
The following characters still won't be interpreted literally in nomagic mode and will need to be escaped with a
$end of line
?(depending on whether you are searching forward or backward) or any other separator that you have chosen if you are searching on the command line
:h nomagic or
An easy way to 'cheat', particularly for paths or other text containing forward slashes, is to use
? instead of