I use C-M-s to search for a regexp. Then I use M-x replace-regexp to replace the same regexp with some string. Can I save typing the regexp for the second time?

Generally, when typing an argument for a command, and then the same argument for a different command, can I save typing the argument for the second time?

  • 2
    Related/duplicate: emacs.stackexchange.com/q/8144/115 Feb 15, 2015 at 13:19
  • just curious, regexp search is it C-s (or) C-M-s?
    – Saravana
    Feb 16, 2015 at 11:08
  • Hi Tim, the previous title was a duplicate of the question linked by @kaushalmodi. However, since the body asked specifically about C-M-s, there's a better possible answer that doesn't show up in the linked question. As such, I've edited your title so as to not be a duplicate, but you're free to rollback the edit if you're not happy.
    – Malabarba
    Feb 16, 2015 at 11:39
  • @MadhavanKumar By default, C-M-s is bound to isearch-forward-regexp (regexp search). The same can also be invoked by doing C-u C-s. C-s is isearch-forward (no regexp). Feb 16, 2015 at 19:55

1 Answer 1


I use C-M-s to search for a regexp. Then I use M-x replace-regexp. Can I save typing the regexp for the second time?

Yes, instead of exiting the incremental search, just hit M-% (which, by the way, is the global binding for query-replace). This will use the search string you've just typed and only prompt you for a replacement.

This will work both on isearch-forward-regexp (C-M-s) and on regular isearch (C-s), and it will inherit the type of search you were using (regexp and non-regexp respectively). You can also use C-M-%, which does the same thing except it uses regexps regardless of the type of isearch you were in.

  • Question: what if I have already exited the search? How can I bring out the query in M-x replace-regexp?
    – Tim
    Feb 28, 2015 at 0:25
  • @Tim start the search again, hit M-p, then follow the above.
    – Malabarba
    Feb 28, 2015 at 10:38
  • when I use M-x occur to do regex search, M-% doesn't switch to regrex replace. Any way?
    – Tim
    Feb 28, 2015 at 19:27
  • @Tim not that I'm aware, but it's worth a feature request.
    – Malabarba
    Feb 28, 2015 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.