Following this thread: https://stackoverflow.com/questions/39808820/replacing-on-mouse-selected-area-in-emacs
I can highlight an area, and do replace only on this area. Great! Now, I want to do several replacements...
The issue is that after I input ! for all occurrences, I need to re-highlight the area again and again. I think that there is a way to avoid doing the re-highlight, but don't know how. Thanks.


One option is to temporarily narrow the buffer to the region. Mark the region you are interested in, and then:

  • C-x n n to narrow-to-region.
  • Jump to the top of the buffer (e.g. M-<)
  • Query/replace (M-%, ... !)
  • Jump back to the top of the buffer (e.g. M-< again, or C-u C-SPC to pop the mark.)
  • Do more query/replace operations, or whatever else you want with the region.
  • When you're done, use C-x n w to widen.
| improve this answer | |

One shortcut that may be useful is C-x C-x exchange-point-and-mark. This will reactivate the last region, except that it extends only so far as the current point. Assuming you left the cursor where it was after the last interactive replacement, that may or may not be all the way to the end of the prior replacement.

If that's not sufficient, you may consider shortcuts to select syntactic units.

  1. C-x h (entire buffer)
  2. M-C-h (current defun)
  3. M-h (paragraph)
  4. Or, use expand-region
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.