5

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?

4

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.

To activate nomagic for a single search, prefix the search pattern with \M.

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 \ (backslash):

  • $ end of line
  • \ escape character
  • / or ? (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

See :h nomagic or :h \m.

  • 1
    Yeah, that's what I thought. Tho prefixing the search pattern \V ("very nomagic") seems better than \M for this. – Kenny Evitt Feb 6 '17 at 20:47
  • Indeed, \V frees up $ (no other difference though it seems). – Endre Both Feb 6 '17 at 20:50
2

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

  • From Vim help: > Use of "\V" means that in the pattern after it only the backslash and the terminating character (/ or ?) has a special meaning. "very nomagic" – Kenny Evitt Feb 6 '17 at 21:21
  • 1
    I am not sure, what you are trying to tell me, but note, that the terminating character refers to the usage of / and ? normal mode commands, which in my example I do not use, so there is no need to escape them. – Christian Brabandt Feb 7 '17 at 6:25
  • I didn't realize that "terminating character" referred to the normal mode command character. – Kenny Evitt Feb 7 '17 at 14:42
1

An easy way to 'cheat', particularly for paths or other text containing forward slashes, is to use ? instead of /.

Source:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.