Use case: I want to:

  1. copy a (long, not handy to manually type) newstring,
  2. replace all occurrences of another (long, not handy to manually type) string by newstring.

Current process (not handy):

  1. Copy (mark + M-w) string in the (main) buffer.
  2. Start the query replace process, using copied string (M-% C-y <RET>).
  3. Select another window (C-x o) to switch from the minibuffer to the main one.
  4. In the (main) buffer, (optional: search (C-s) for newstring and) copy (mark + M-w) newstring.
  5. Select another window (C-x o) to switch from the main buffer to the minibuffer.
  6. Paste (C-y) newstring and continue the query replace process (<RET>).

Possible start of track: This answer provides the a nice way (C-s C-w) to search for the word after the current mark.

Question: I wonder if:

  1. the previous track could be extended to replace for the word after the current mark,
  2. or if there is another, better, track for my use case.

1 Answer 1


Avoiding window switching:

  1. Copy string
  2. Copy newstring
  3. M-%C-yM-yRET
  4. C-yC-u-1M-yRET

Or if you can get used to copy newstring before string:

  1. Copy newstring
  2. Copy string
  3. M-%C-yRET
  4. C-yM-yRET

And another option is to write function which will call query-replace with most recent kill ring entries and bind it to some key. Then just:

  1. Copy string
  2. Copy newstring
  3. key for my/query-replace-function
  • Not as handy as I would expect (I hoped something like in the spirit of linked answer), but works indeed :) Oct 30, 2018 at 17:30
  • C-y M-y is quite handy. You have all of your past kills and copies available in a ring. You can just cycle M-y to get to the one you want. Or you can use Browse Kill Ring to choose any of them. Or you can use menu Edit > Select and Paste to choose one.
    – Drew
    Oct 31, 2018 at 0:07
  • Or you can use the secondary selection for either of the pastes (yanks). The secondary selection stays, even as you change the region or copy/kill other selections.
    – Drew
    Oct 31, 2018 at 0:08

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.