1

How can I quickly toggle between string and regexp versions of functions such as replace-string/replace-regexp, query-replace/query-replace-regexp etc? Pretty sure I saw a shortcut once but now I cant find it.

  • I am not sure I understand what you mean. The replace- functions are not usually bound to any key, while the query-replace- ones are bound to M-% and C-M-%, respectively. Isn't that already quick enough? It seems to me that remembering the state of some toggle just adds to your cognitive burden. – Harald Hanche-Olsen Apr 7 '18 at 14:18
  • I find C-M-% a bit difficult to press so most of the time I go for M-x and type query-replace-regexp manually. Most of the time I use query-replace (via M-%) but sometimes it would be easier to press M-% and then something else to switch to query-replace-regexp instead. – ext Apr 7 '18 at 14:27
  • In that case, I'd be tempted to turn M-% into a prefix key and bind query-replace to M-% s (s for string) and query-replace-regexp to M-% r. – Harald Hanche-Olsen Apr 7 '18 at 14:43
5

Like Harald, I'm not sure what kind of answer you expect.

But FWIW, I think the best way to perform such operations is with:

C-s <string1> M-% <string2> RET

in which case the answer is that you can use M-r any time between the C-s and the M-% to toggle between string search and regexp search. You can similarly use M-c to toggle the case-sensitivity.

  • 2
    I was not aware of the power of isearch-forward. But it looks like M-r only toggles the search mode, while you still must use C-M-% to run query-replace-regexp? (Unless the docstring for isearch-forward is wrong.) What would be nice is if M-% in this context chose between the two replace commands to match the current search mode – oh, wait, experimentation reveals that it does so! That's a documentation bug, then. – Harald Hanche-Olsen Apr 7 '18 at 15:03
  • 1
    Also, if you know you want to start with a regexp isearch and you find C-M-s awkward, by default you can alternatively use C-u C-s. Although depending on your keyboard C-s M-r may still be easier than either of those. – phils Apr 7 '18 at 22:23
  • 1
    @HaraldHanche-Olsen I just tested it and M-% does start query-replace-regexp if you use it from a regexp isearch. – Omar Sep 20 '18 at 15:01

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.