I'd like to define a command that initiates an isearch-forward-regexp with a predefined regexp.
Say I want to find all occurrences of either "aaa" or "bbb". The regexp which I can enter interactively with isearch-forward-regexp is \\(aaa\\|bbb\\)
Is there any way to do this without entering the regexp interactively? I've tried things like this:
(defun my-search ()
(interactive)
(isearch-mode t t)
(isearch-yank-string "\\(aaa\\|bbb\\)"))
but this approach doesn't seem to work because there doesn't seem to be any way to define a string in emacs that evaluates to \\(aaa\\|bbb\\) when yanked into isearch.
For instance "\\(aaa" evaluates to (aaa, but "\\\\(aaa" evaluates to \\\\(aaa. There doesn't seem to be a way to evaluate to a single backslash.
Is there a way of doing what I want?
This command should do what I think you are asking for:
For example:
M-x foo RET, then enter\(aaa\|bbb\) RET.If you call it from Lisp instead of interactively then just provide the regexp you want as a string. For example:
Note that
isearch-yank-stringdoes this:That's why
foobindsisearch-regexptonilaround the call toisearch-yank-string: to prevent quoting regexp special chars in the string.