2

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?

Malabarba
  • 22,878
  • 6
  • 78
  • 163
Tim
  • 4,987
  • 7
  • 31
  • 60
  • 2
    Related/duplicate: http://emacs.stackexchange.com/q/8144/115 – Kaushal Modi Feb 15 '15 at 13:19
  • just curious, regexp search is it `C-s` (or) `C-M-s`? – Madhavan Feb 16 '15 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 '15 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). – Kaushal Modi Feb 16 '15 at 19:55

1 Answers1

5

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.

Malabarba
  • 22,878
  • 6
  • 78
  • 163