6

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"?

Drew
  • 75,699
  • 9
  • 109
  • 225
user2699
  • 2,181
  • 16
  • 32

1 Answers1

5

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!
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • Plain arrow-up also works. – Lindydancer Feb 11 '15 at 13:22
  • I'm was hoping for a more automatic solution, but this does work. – user2699 Feb 11 '15 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](https://github.com/magnars/hardcore-mode.el). – Kaushal Modi Feb 11 '15 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 – Kaushal Modi Feb 11 '15 at 14:39
  • It's worth noting that these instructions will change in Emacs 25. Namely, a single `M-p` will bring up both previous inputs simultaneously. – Malabarba Feb 15 '15 at 14:31
  • 1
    It's also possible to use different histories for to and from using `query-replace-{to,from}-history-variable`. – politza Feb 15 '15 at 20:36
  • @Malabarba: I came here to find out how, in Emacs25, I can choose `thing1` only but change the replacement string. – Adrian Pronk Oct 20 '18 at 07:18