2

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?

Sean
  • 939
  • 4
  • 13
  • @Drew I specified "regexp (i)search" multiple times. – Sean Feb 04 '21 at 20:38
  • 1
    Another workaround (still far from ideal): start a regular search with C-s, yank your pattern with C-y then toggle regex on with M-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 run C-M-s, C-s now it will work. – Ian Mackinnon May 11 '21 at 09:31

1 Answers1

1

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

Documentation:

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.

Drew
  • 77,472
  • 10
  • 114
  • 243