Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer
1  
Yeah, that's what I thought. Tho prefixing the search pattern \V ("very nomagic") seems better than \M for this. – Kenny Evitt 13 hours ago
    
Indeed, \V frees up $ (no other difference though it seems). – Endre Both 13 hours ago

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

share|improve this answer
    
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 12 hours ago
    
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 3 hours ago

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

Source:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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