1

As stated in the title, I want to search and edit (including replace) certain strings in a selected part of a buffer, not the entire buffer!!!, how can I do it? Are there any built-in keystrokes?

  • 2
    If you first mark a region and afterwards call query-replace-regexp only the hits within region are found and replaced. You can also narrow the buffer to the region of interest before running query-replace-regexp. – Tobias Jan 14 at 4:39
  • I tried this, it works, thank you so much. BTW, for those may run into this later, be sure to read the part about how to continue after you input the string patterns. – Jason Goal Jan 14 at 12:11
2
  1. Vanilla Emacs has query-replace and related commands, that stop successively at each match for your search pattern and ask whether you want to replace it, then move on to the next match. When the region is active then these commands limit searching to the region.

  2. You can always narrow the buffer (C-x n n) to the region, and then use ordinary Isearch.

  3. If you use library Isearch+ then you can search the active region without having to narrow to it, so nothing is hidden.

  4. If you also use library Zones, then Isearch+ also lets you search within multiple regions (zones). That is, the area to search need not be contiguous.

  5. If you use Isearch+ then you can also replace any given matches, on demand. Unlike query-replace commands, which necessarily ask you about replacing each search hit in turn (or replacing all subsequent hits at once), on-demand means that when you are incrementally searching you can replace the current search hit by hitting a key.

| improve this answer | |
  • Hi Drew, query-replace works on region in vanilla Emacs. You do not have to narrow to region. (BTW: All the best for 2020!) – Tobias Jan 14 at 7:11
  • @Drew, I'll try this out later. – Jason Goal Jan 14 at 12:29
  • @Tobias: That's exactly what I said: "Vanilla Emacs... When the region is active then these commands limit searching to the region." (And Happy New Year to you, too, Tobias!) – Drew Jan 14 at 17:41
  • @Drew, ah I see you do an isearch in the current region and not a query-replace. So I posted too hasty. – Tobias Jan 14 at 20:36
  • @Tobias: The different points I listed are different possibilities. The first two need only vanilla Emacs. The first is search and replace; the second is only search. – Drew Jan 14 at 23:09

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.