3

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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Jason Goal
  • 153
  • 6
  • 2
    If you first [mark a region](https://www.gnu.org/software/emacs/manual/html_node/emacs/Mark.html) and afterwards call [`query-replace-regexp`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Query-Replace.html) only the hits within region are found and replaced. You can also [narrow the buffer](https://www.gnu.org/software/emacs/manual/html_node/emacs/Narrowing.html) to the region of interest before running `query-replace-regexp`. – Tobias Jan 14 '20 at 04: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 '20 at 12:11

2 Answers2

5
  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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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 '20 at 07:11
  • @Drew, I'll try this out later. – Jason Goal Jan 14 '20 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 '20 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 '20 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 '20 at 23:09
  • I still do not get it: when I just want to use the normal search+replace option that every editor has, and I do not want to `Enter` through all of the found cases, and I want to do this firstly in a selection and secondly in the whole file, what would I do in emacs? I ended up in using `Alt`+`x`:"query-replace", entering the "search word", entering the "replace word", and then `Enter` a lot of times. This cannot be the end of the story :). – questionto42 Jan 04 '22 at 21:07
  • @questionto42: It's not clear to me what you're asking, but it doesn't seem related to the question posted here. It sounds kinda like you don't want to repeat hitting a key to visit each search hit. Consider asking that as a separate question: "How can I search without visiting each search hit", or some such, depending on what you really want to know. – Drew Jan 04 '22 at 22:59
  • Thanks, that is [done](https://emacs.stackexchange.com/questions/70005/how-can-i-search-and-replace-without-visiting-each-search-hit). – questionto42 Jan 04 '22 at 23:13
1

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.