* How to search inside a region that has just been used as the target of a replace operation?
@ 2023-01-27 8:55 Evan Aad
2023-01-27 9:28 ` Anders Munch
0 siblings, 1 reply; 5+ messages in thread
From: Evan Aad @ 2023-01-27 8:55 UTC (permalink / raw)
To: help-gnu-emacs
I'd like to be able to search inside a region that has just been used
as the target of a replace operation, without having to specify
explicitly the region's start and end points.
Here's a concrete usage scenario:
1. Initial state: the current buffer contains the text "ABCBD", and
the cursor is on 'A'.
2. Select the region "BCB": C-f C-SPC C-f C-f C-f.
3. Replace all occurrences of the string "C" inside the selected
region: M-x replace-string, C, c.
The buffer's content is now "ABcBD".
Goal: Replace all occurrences of the string "B" inside the region
selected previously by the string "b", so that, at the end of the
operation, the buffer's content will be "AbcbD".
I'd like to accomplish the goal without having to explicitly specify
the region's start and end points.
How can this be accomplished?
Attempt no. 1, the naive approach:
4. Replace all occurrences of the string "B" by the string "b"
starting from the state that was in effect at the end of step no. 3:
M-x replace-string, B, b.
In the end the buffer's content was "ABcbD". The goal is not met,
because the first occurrence of 'B' was not replaced, even though it
was part of the original region.
Attemp no. 2, reactivate the region:
4. Reactivate the region: C-x C-X.
5. Replace all occurrences of the string "B" by the string "b": M-x
replace-string, B, b.
In the end the buffer's content was "AbcBD". The goal is not met,
because the second occurrence of 'B' has not been replaced, even
though it was part of the original region.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to search inside a region that has just been used as the target of a replace operation?
2023-01-27 8:55 How to search inside a region that has just been used as the target of a replace operation? Evan Aad
@ 2023-01-27 9:28 ` Anders Munch
2023-01-27 10:44 ` Evan Aad
0 siblings, 1 reply; 5+ messages in thread
From: Anders Munch @ 2023-01-27 9:28 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org
Evan Aad wrote:
> I'd like to be able to search inside a region that has just been used as the target of a replace operation, without having to specify explicitly the region's start and end points.
Not a direct answer to your question, but I wonder if you are aware of narrow-to-region?
3. C-x n n (narrow-to-region)
4. M-< (beginning-of-buffer, which now goes to the beginning of the narrowed region)
5. M-x replace-string C -> c
6. M-<
7. M-x replace-string B -> b
8. C-x n w (widen, undoes the narrowing)
narrow-to-region is disabled by default. For good reason. It's very unnerving if you invoke it by accident and don't understand what's happening, and suddenly most of your file is mysteriously gone (by appearance).
But it's also extremely useful.
regards, Anders
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to search inside a region that has just been used as the target of a replace operation?
2023-01-27 9:28 ` Anders Munch
@ 2023-01-27 10:44 ` Evan Aad
2023-01-27 14:35 ` Evan Aad
2023-01-27 18:11 ` Marcin Borkowski
0 siblings, 2 replies; 5+ messages in thread
From: Evan Aad @ 2023-01-27 10:44 UTC (permalink / raw)
To: Anders Munch; +Cc: help-gnu-emacs@gnu.org
Anders Munch wrote:
> I wonder if you are aware of narrow-to-region?
No, I wasn't aware of it. This solves my problem. This thread can be
closed with status "Answered". (However, other answers and comments
are still welcome.) Thanks, Anders!
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to search inside a region that has just been used as the target of a replace operation?
2023-01-27 10:44 ` Evan Aad
@ 2023-01-27 14:35 ` Evan Aad
2023-01-27 18:11 ` Marcin Borkowski
1 sibling, 0 replies; 5+ messages in thread
From: Evan Aad @ 2023-01-27 14:35 UTC (permalink / raw)
To: Anders Munch; +Cc: help-gnu-emacs@gnu.org
Another solution is to save the position of the point in a register prior
to performing the replacement:
2.5 (Add the following between steps 2 and 3 of the scenario described
in the original question:) C-x r SPC p (This saves the position of the
point in the register p. Any other character can be used in lieu of
'p'.)
4. C-x C-x (This moves the point to the beginning of the region
established in step no. 2.
5. C-SPC (This sets the mark at the position of the point.)
6. C-x r j p (This moves the point to the position saved in the
register 'p'. The region's content is now "BcB".
7. M-x replace-string, B, b. (The buffer's content is now "AbcbD", as desired.)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to search inside a region that has just been used as the target of a replace operation?
2023-01-27 10:44 ` Evan Aad
2023-01-27 14:35 ` Evan Aad
@ 2023-01-27 18:11 ` Marcin Borkowski
1 sibling, 0 replies; 5+ messages in thread
From: Marcin Borkowski @ 2023-01-27 18:11 UTC (permalink / raw)
To: Evan Aad; +Cc: Anders Munch, help-gnu-emacs@gnu.org
On 2023-01-27, at 11:44, Evan Aad <oddeveneven@gmail.com> wrote:
> Anders Munch wrote:
>> I wonder if you are aware of narrow-to-region?
>
> No, I wasn't aware of it. This solves my problem. This thread can be
> closed with status "Answered". (However, other answers and comments
> are still welcome.) Thanks, Anders!
`narrow-to-region' (and other narrowing commands, see e.g. `C-x n C-h')
are indeed /very/ useful. I didn't even remember they are disabled by
default -- I enabled them a long, long time ago and use them all the
time. (Org mode has its own share of them, by the way.)
But if you happen to do your machinations pretty often, I would
recommend writing a custom Elisp function to do it. If you don't know
Elisp, you might want to read Robert J. Chassell's "An Introduction to
Programming in Emacs Lisp". Place the point after this form: (info
"eintr") and press `C-x C-e' or head to
https://www.gnu.org/software/emacs/manual/eintr.html.
Hth,
--
Marcin Borkowski
http://mbork.pl
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-01-27 18:11 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-27 8:55 How to search inside a region that has just been used as the target of a replace operation? Evan Aad
2023-01-27 9:28 ` Anders Munch
2023-01-27 10:44 ` Evan Aad
2023-01-27 14:35 ` Evan Aad
2023-01-27 18:11 ` Marcin Borkowski
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.