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
    Commented Jan 14, 2020 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
    Commented Jan 14, 2020 at 12:11

2 Answers 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.

  • 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
    Commented Jan 14, 2020 at 7:11
  • @Drew, I'll try this out later.
    – Jason Goal
    Commented Jan 14, 2020 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
    Commented Jan 14, 2020 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
    Commented Jan 14, 2020 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
    Commented Jan 14, 2020 at 23:09

I would think that a simple region based vi[m] search and replace should do the trick. Eg to indent a region with 2 spaces

  1. Mark start of region using marker k - mk
  2. Move a couple of lines down for the desired range
  3. Do the vim replace for the region - :'k,.g/^/s// /

but that results in 'Replaced 0 occurences'. This works 100% in viper mode.

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.