5

I already know that to redo a change, I need to type something like C-g C-/.

Why does it start to undo after all of the possible redos are done?

Here is what I mean:

  1. Visit a new file.

  2. Type

    aa
    
    bb
    
    cc
    
  3. Type C-/ five times to undo the text.

  4. Type C-g (one time) and then C-/ (five times) to redo the text.

  5. Continue to type C-/. I expected that it wouldn't make any effect, since there are no more things to redo, but it started to delete the text, as in the third step.

Why? And how can I prevent it from happening?

0

2 Answers 2

8

The reason Emacs "starts to undo after all of the possible redos are done" is that, after you interrupted the original undo sequence with C-g, every change you had made up until that point became part of the tree to be undone. Those changes include not only the initial text you entered ("aa bb cc") but also the undoing of those changes. As the manual explains: "Any command other than an undo command breaks the sequence of undo commands. Starting from that moment, the entire sequence of undo commands that you have just performed are themselves placed into the undo record."

This is not how undo works in most other text editors, and some people find this behavior counterintuitive. However, this type of undo is very powerful, since it allows you to recover any past state of a buffer. You may want to try the package undo-tree, which replaces the native undo Emacs feature with a more intuitive equivalent, without sacrificing its power. Its commands undo-tree-undo and undo-tree-redo will behave like the undo and redo you are used to, but you can also see a visualization of the undo tree branches (undo-tree-visualize), and switch branches if needed (undo-tree-switch-branch).

ADDED: Check the comments to this answer by ideasman42 and Phil Hudson for some issues with undo-tree, and note that as ideasman42 mentions in another answer, Emacs 28 offers the command undo-redo out of the box.

7
  • Undo tree has long standing undo corruption problems emacs.stackexchange.com/q/31438/2418
    – ideasman42
    Aug 3, 2021 at 1:31
  • 1
    tl;dr: Go ahead and use undo-tree, but make sure you allocate plenty of memory to undo-limit and any related options. Longer explanation: I believe that the root cause of the corruption problem mentioned by @ideasman42 has finally been identified. IIRC, it has now been shown that this is not an undo-tree issue, but rather a defect in memory allocation that is built in to Emacs and is only exposed by undo-tree. Aug 3, 2021 at 20:42
  • Found the reference: dr-qubit.org/Lost_undo-tree_history.html Aug 3, 2021 at 20:49
  • @phil-hudson whether the author of undo tree is responsible for the problem or not, undo corruption is pretty serious (for me at least), that I'm not sure why anyone would risk it by using a complex package that is known to have problems corrupting undo.
    – ideasman42
    Aug 4, 2021 at 4:08
  • 1
    @ideasman42 I don't want to get in a fight over this. I think the linked article makes it clear that the problem exposed by undo-tree can strike without undo-tree being installed. The statement that the "package is known to have problems corrupting undo" is false, unless you know something that contradicts what is in the article. It is true that the package was long suspected and widely believed to have such problems. That's not the same thing. I'm just trying to dispel what I currently believe, on the basis of the evidence available to me, to be a myth. Aug 5, 2021 at 9:09
5

Emacs 28 adds a redo command (called undo-redo).

If you want to have more typical undo/redo, the following commands can be used.

(global-set-key (kbd "C-z") 'undo-only)
(global-set-key (kbd "C-S-z") 'undo-redo)

You can also check the package undo-fu which has this functionality, allowing to access the full non-linear undo history as well.

3
  • This is great, but I'm not sure I understand where Emacs 28 can be downloaded. Do you build it from sources?
    – user90726
    Aug 4, 2021 at 12:43
  • 1
    It's not released yet, the undo-fu package contains a back-ported version of 28's redo feature.
    – ideasman42
    Aug 5, 2021 at 0:38
  • 1
    Thanks. And thanks a lot for this package.
    – user90726
    Aug 5, 2021 at 14:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.