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?

user90726
  • 173
  • 7

2 Answers2

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.

Pablo
  • 184
  • 1
  • 7
  • Undo tree has long standing undo corruption problems https://emacs.stackexchange.com/q/31438/2418 – ideasman42 Aug 03 '21 at 01: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. – Phil Hudson Aug 03 '21 at 20:42
  • Found the reference: https://www.dr-qubit.org/Lost_undo-tree_history.html – Phil Hudson Aug 03 '21 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 04 '21 at 04: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. – Phil Hudson Aug 05 '21 at 09:09
  • @phil-hudson whatever the case, I stopped running into problems with my undo stack getting corrupt when I stopped using undo-tree. I'm not arguing on some technical level it may be inherent with the undo information getting garbage collected or some such thing. But a lot if people ran into problems with undo-tree, over many years, there are quite a few reports on this. Further, whenever I mentioned this, there were suggestions to make tweaks to my config - none of them worked. I was burned badly by this issue, loosing work multiple times. – ideasman42 Aug 05 '21 at 13:29
  • I understand your wariness. However, anecdotal reports of corruption correlating with undo-tree have surfaced for a long time with no reproducible test case. Correlation is not causation. There is no evidence that undo-tree triggers what is probably an Emacs GC problem, against which undo-tree now offers partial protections. Suspicions about undo-tree may yet prove correct, but for now we should allocate more memory to undo, whether we use undo-tree or not. It's OK to say that suspicions exist, but we should not overstate what we know. Recent undo-tree might even be safer than vanilla undo. – Phil Hudson Aug 06 '21 at 10:54
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.

ideasman42
  • 8,375
  • 1
  • 28
  • 105