I noticed today that when I yank a string containing regexp metacharacters into an active regexp isearch, the metacharacters all get escaped. For example, if I've killed the text foo* and yank that string into a search, it becomes foo\*.

My actual use case involved me generating a list of alternatives I wanted to search for in a temporary buffer, and then joining them together with \|. Something like foo\|bar\|...\|baz. In the buffer I wanted to search, I started a regexp search and pasted in that string, but it became foo\\|bar\\|...\\|baz.

I haven't been able to find this behavior described in the docs. I got around it by calling eval-expression and providing my string to search-forward-regexp, but that's obviously kind of a hassle, and moreover doesn't let me repeat the search with a simple C-s.

The behavior I described doesn't seem correct to me, or at least not intuitive, but is it documented somewhere? If it is correct, are there any other reasonable workarounds?

  • @Drew I specified "regexp (i)search" multiple times. – Sean Feb 4 at 20:38

You have this option with Isearch+: isearchp-regexp-quote-yank-flag:

isearchp-regexp-quote-yank-flag is a variable defined in isearch+.el.

Its value is t


Non-nil means escape special chars in text yanked for a regexp isearch.

You can toggle this using M-= ` during Isearch.

You can customize this variable.

Note that though this is a user option, you can toggle its value anytime during Isearch. The option value, in effect, specifies the default behavior.

Code is here.

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.