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?
C-s
, yank your pattern withC-y
then toggle regex on withM-r
. At this point highlighting works, but when you hit enter the search will fail. However, the pattern is now in the regex search history and if you runC-M-s
,C-s
now it will work.