Most functions in emacs keep a default option, with the last arguments used. Is it possible to reuse some of the default arguments while changing others?

For example, when using search and replace, a string such as Query replace (default thing1 -> thing2) lets you reuse the last command, instead of typing it all over. Is there a way to keep "thing1" as the first argument, but enter a new string in place of "thing2"?

1 Answer 1


Let's say you already did a query-replace of thing1 with thing2.

Here is how it goes if you want to use thing1 as the first argument again. It is not automatic but you don't have to type in the whole thing1 either. The benefit of this approach is more more when doing query-replace-regexp and you need to repeat a very complex regexp.

  • Call query-replace or query-replace-regexp again.
  • Entering search expression: M-p M-p (yes, twice) - The first M-p will bring up thing2 and the second will bring up thing1.
  • Entering replace expression: Proceed with your use case where you want to enter a new string instead of thing2. Or if you change your mind and want to continue with thing2, do M-p M-p again!
  • Plain arrow-up also works. Feb 11, 2015 at 13:22
  • I'm was hoping for a more automatic solution, but this does work.
    – user2699
    Feb 11, 2015 at 14:25
  • @Lindydancer Thanks for adding that. I wouldn't have known as I have got used to not using the arrow keys, thanks to hardcore-mode. Feb 11, 2015 at 14:27
  • 2
    @user2699 Emacs automatically populates the thing1 and thing2. Hitting M-p in the minibuffer will become instinctive once you realize you can do M-p in almost all the cases where you want to recall the previous string and want to save typing. I use it in isearch, when renaming files, query-replace(-regexp), etc Feb 11, 2015 at 14:39
  • 1
    It's also possible to use different histories for to and from using query-replace-{to,from}-history-variable.
    – politza
    Feb 15, 2015 at 20:36

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.