Suppose we start with the following

Adam ate an apple. 

Adam ate an apple.

Adam ate an apple.

Adam ate an apple.

Adam ate an apple.

I want to replace Adam with Bob, Chris, and David. So I want to do it such that I call a query-replace-like function, search for Adam and then cycle through the results specifying what to replace it by.

I am guessing there are three possible answers here:

  1. Write a function to do it, which I haven't tried.
  2. There is a way to do it using query-replace-like function which is what I am looking for.
  3. I realize that I could do C-r during query-replace, but I was wondering if there was a way to avoid that.

1 Answer 1


Assuming Bob is the first replacement:

  • M-% Adam RET Bob RET
  • y for all the consecutive Bob replacements
  • e to change the replacement string, to, e.g.: Chris RET
  • y for any further Chris replacements
  • etc...
  • When you want to re-use one of the previous replacements (say, Adam again)
  • e and then M-p or <up> to cycle back through the replacement history

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.