4

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?

3 Answers 3

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

3
  • 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" Commented Feb 6, 2017 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. Commented Feb 7, 2017 at 6:25
  • I didn't realize that "terminating character" referred to the normal mode command character. Commented Feb 7, 2017 at 14:42
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.

2
  • 1
    Yeah, that's what I thought. Tho prefixing the search pattern \V ("very nomagic") seems better than \M for this. Commented Feb 6, 2017 at 20:47
  • 1
    Indeed, \V frees up $ (no other difference though it seems).
    – Endre Both
    Commented Feb 6, 2017 at 20:50
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 and acknowledge you have read our privacy policy.

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