0

I'm trying to use undo in a region to undo multiple changes -- but I can only toggle between undoing one change and redoing it.

For example, if I start with:

Here is line 1 with some text.

Line 2 is here.

Line 3 is, naturally enough, after line 2.

I make a change first to line 1, then to line 3:

Here is line 1 with some FIRST-CHANGE text.

Line 2 is here.

Line 3 is, naturally enough, after line 2. SECOND-CHANGE

Then I go back and change line 1 again:

Here is line 1 with some FIRST-CHANGE text. And THIRD CHANGE too!

Line 2 is here.

Line 3 is, naturally enough, after line 2. SECOND-CHANGE

I want to undo all the changes to line 1 but leave the others. So I select a region with just line 1 and do C-/ to undo in region. That just removes the final exclamation point:

Here is line 1 with some FIRST-CHANGE text. And THIRD CHANGE too

Line 2 is here.

Line 3 is, naturally enough, after line 2. SECOND-CHANGE

Okay, I want to keep going. I select line 1 again, do C-/ again, and get the exclamation point back!

If I use regular undo, I can just keep undoing through previous changes.

Is there a way to do the same with a region?

I've tried undo-redo but that doesn't do what I want.

Is there a way to undo everything in line 1 but keep line 3 as it is?

0

1 Answer 1

2

The cause of the problem is:

I select line 1 again

You don't need to re-select the region every time (which would be a very cumbersome requirement) -- Emacs knows to continue undoing in that region. Just select the region once and then type C-/ as many times as necessary.

The act of selecting a region breaks the undo chain (it's not an undo command), and so the subsequent undo is starting a new chain by undoing the most recent change (which was an undo -- hence the "redo"). (You maybe then repeated that cycle creating even more copies of the undo/redo sequence to move through.)


Note that you can also use M-x undo-only to undo without undoing undo commands in the list. If you then repeat that (C-xzzzz...) you'll get back to where you want to be quickly, even if you'd accidentally introduced some "redos" into the undo list along the way.


I've tried undo-redo but that doesn't do what I want.

For clarity, that command performs a "redo" (which was what you were trying to avoid doing).

2
  • Ah, that's it! I didn't expect that emacs would perform undo-in-region when there wasn't an explicit region selected, but it does.
    – Dan Drake
    Commented Nov 1 at 15:56
  • Remember that there's (almost) always a current region between mark and point, even when the region isn't active (highlighted). Lots of commands act on the current region, whether it's active or not. In this case it's necessary to have an active region as an initial step, but not for the subsequent undos.
    – phils
    Commented Nov 1 at 23:35

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.