ChatGPT解决这个技术问题 Extra ChatGPT

How do you 'redo' changes after 'undo' with Emacs?

This article says that "Emacs has redo because you can reverse direction while undoing, thereby undoing the undo".

What does this mean? How can a user 'redo' with Emacs?

The answer beneath the accepted answer tells you how, if that's why you're looking at this answer, which, given how it's written, I would expect most people to be actually looking for that.
I just hit the space bar, and then undos after that undo the space and redo the undos you did until it gets to the end and then starts going back again... I know there are much more complicated ways, but this is fast and easy, and that's how I roll.
Emacs 28 now supports this, see: stackoverflow.com/a/60163018/432509

M
Michael Ekstrand

Short version: by undoing the undo. If you undo, and then do a non-editing command such as C-f, then the next undo will undo the undo, resulting in a redo.

Longer version:

You can think of undo as operating on a stack of operations. If you perform some command (even a navigation command such as C-f) after a sequence of undo operations, all the undos are pushed on to the operation stack. So the next undo undoes the last command. Suppose you do have an operation sequence that looks like this:

Insert "foo" Insert "bar" Insert "I love spam"

Now, you undo. It undoes the last action, resulting in the following list:

Insert "foo" Insert "bar"

If you do something other than undo at this point - say, C-f, the operation stack looks like this:

Insert "foo" Insert "bar" Insert "I love spam" Undo insert "I love spam"

Now, when you undo, the first thing that is undone is the undo. Resulting in your original stack (and document state):

Insert "foo" Insert "bar" Insert "I love spam"

If you do a modifying command to break the undo sequence, that command is added after the undo and is thus the first thing to be undone afterwards. Suppose you backspaced over "bar" instead of hitting C-f. Then you would have had

Insert "foo" Insert "bar" Insert "I love spam" Undo insert "I love spam" Delete "bar"

This adding/re-adding happens ad infinitum. It takes a little getting used to, but it really does give Emacs a highly flexible and powerful undo/redo mechanism.


Just in case this sounds annoying, the major advantage is that you can always get back a previous state. In most editors, undoing several changes and then accidentally typing a character would leave you 'stranded' with no way to redo what you had undone. Emacs makes this trivial.
I suppose in your construction, if a person wanted to do it on paper,you'd need two stacks.Because if for example, at the point where you wrote "Now, you undo. It undoes the last action, resulting in the following list:" you (understandably) didn't show the undo, and if he had done many undos, you wouldn't show them until something was done to break the undo sequence,then you'd add them. Another way to show it would be to have one list,and to add undos to the list even before they're broken then but to note where the cursor is in the list,so we know where the undos are working backwards from
@barlop Late reply, but at least from a user-visible perspecive, undos are only on the stack after the sequence is broken (otherwise, 'undo' repeatedly would just toggle the last action). This may not be how it is implemented (e.g. it could use some kind of pointer to a queue or something), but I am trying here to explain the user-visible behavior.
I don't know why this answer was accepted when it doesn't even tell you HOW.
Sadly this answer doesn't say anything about the redo command which is C-g C-/.
p
programking

To undo: C-_

To redo after a undo: C-g C-_

Type multiple times on C-_ to redo what have been undone by C-_ To redo an emacs command multiple times, execute your command then type C-xz and then type many times on z key to repeat the command (interesting when you want to execute multiple times a macro)


To repeat stuff you can also use C-u, for instance C-u 8 * (inserts eight * where the caret is). So you can use C-u x C-_ to undo x steps.
This has nothing to do with the original question, but right after executing a keyboard macro (with C-x e), you can just press e as often as you need to execute it again.
Also, C-/ and C-x u are alternative undo key bindings, equivalent to C-_. I prefer C-/ since it's easier to hit.
Warning: when this answer says that C-g C-_ will "redo after an undo", that only applies the first time you hit the combination. After that, repeat your undo key without C-g. So to redo three times right after undoing, type C-g C-_ C-_ C-_. If you type C-g C-_ C-g C-_ C-g C-_, you will just go around in circles, undoing your redo of your undo...
@RoryO'Kane I don't know much about emacs, so please correct me where i'm wrong, but given that C-g C-_ doesn't have any effect after a redo, could it be that is because it only operates after an undo(like your descrption "redo after an undo" suggests. So it's not so much that it's going in circles redoing an undo or a redo e.t.c. it's not doing anything because an undo wasn't just done before it. so C-g C-_ C-g C-_ is not going in a circle after the first one, it's literally doing nothing.
R
Rory O'Kane

To undo once: C-/

To undo twice: C-/ C-/

To redo once, immediately after undoing: C-g C-/

To redo twice, immediately after undoing: C-g C-/ C-/. Note that C-g is not repeated.

To undo immediately again, once: C-g C-/

To undo immediately again, twice: C-g C-/ C-/

To redo again, the same…

If you have pressed any keys (whether typing characters or just moving the cursor) since your last undo command, there is no need to type C-g before your next undo/redo. C-g is just a safe key to hit that does nothing on its own, but counts as a non-undo key to signal the end of your undo sequence. Pressing another command such as C-f would work too; it’s just that it would move the cursor from where you had it.

If you hit C-g or another command when you didn’t mean to, and you are now undoing in the wrong direction, simply hit C-g to reverse your direction again. You will have to undo all the way through your accidental redos and undos before you get to the undos you want, but if you just keep hitting C-/, you will eventually reach the state you want. In fact, every state the buffer has ever been in is reachable, if you hit C-g once and then press C-/ enough times.

Alternative shortcuts for undo, other than C-/, are C-_, C-x u, and M-x undo.

See Undo in the Emacs Manual for more details on Emacs’s undo system.


T
Trey Jackson

For those wanting to have the more common undo/redo functionality, someone has written undo-tree.el. It provides the look and feel of non-Emacs undo, but provides access to the entire 'tree' of undo history.

I like Emacs' built-in undo system, but find this package to be very intuitive.

Here's the commentary from the file itself:

Emacs has a powerful undo system. Unlike the standard undo/redo system in most software, it allows you to recover any past state of a buffer (whereas the standard undo/redo system can lose past states as soon as you redo). However, this power comes at a price: many people find Emacs' undo system confusing and difficult to use, spawning a number of packages that replace it with the less powerful but more intuitive undo/redo system. Both the loss of data with standard undo/redo, and the confusion of Emacs' undo, stem from trying to treat undo history as a linear sequence of changes. It's not. The `undo-tree-mode' provided by this package replaces Emacs' undo system with a system that treats undo history as what it is: a branching tree of changes. This simple idea allows the more intuitive behaviour of the standard undo/redo system to be combined with the power of never losing any history. An added side bonus is that undo history can in some cases be stored more efficiently, allowing more changes to accumulate before Emacs starts discarding history.


Fantastic! Who said emacs undo was unfathomable? :-)
Amazing! conceptually sound and user intuitive. This should be in the default packages. One comment: I wish some alert message was also shown when redoing from a branch point, not just when reverting to it. For example, when user calls a redo: 'Redoing from a branch point' or 'Default redo branch used' - just to alert the user that there are more than one branches.
Another approach to this problem that is much simpler to implement and doesn't require a visualizer is to keep ONLY the last branch point, and provide 3 commands: undo, redo, switch-branch. Undo/redo will alert when THE branch is hit, and switch-branch will just switch if at a branch else just issue a 'not at a branch' message. If branching occurs at any time, the previous branch point is lost. This would resolve the state loss problem reasonably well and at minimal complexity.
Beware this has a long standing bug in redo which can randomly fail: github.com/syl20bnr/spacemacs/issues/9903 - known about since 2014
@ideasman42 Apparently this is a "non-bug": dr-qubit.org/Lost_undo-tree_history.html
M
Micah Elliott

Beware of an undo-tree quirk for redo!

Many popular “starter kits” (prelude, purcell, spacemacs) come bundled with undo-tree. Most (all?) even auto-enable it. As mentioned, undo-tree is a handy way to visualize and traverse the undo/redo tree. Prelude even gives it a key-chord (uu), and also C-x u.

The problem is: undo-tree seems to wreck Emacs’ default and well-known binding for redo: C-g C-/.

Instead, you can use these symmetrical keys for undo/redo:

C-/     undo
C-S-/   redo

These are useful since sometimes you want to quickly redo without opening up the visualizer.


This. I am using Spacemacs and couldn't figure out why it wasn't working for me. Thanks!
i
ideasman42

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)

To access this functionality (with the ability to use non-linear history too, see undo-fu which is a thin wrapper on emacs built in undo).


m
monotux

I find redo.el extremly handy for doing "normal" undo/redo, and I usually bind it to C-S-z and undo to C-z, like this:

(when (require 'redo nil 'noerror)
    (global-set-key (kbd "C-S-z") 'redo))

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

Just download the file, put it in your lisp-path and paste the above in your .emacs.


The comments in the header of redo.el say that it is made for XEmacs. Is there any problem using it in GNU Emacs and other related Emacsen?
I haven't had any problems yet, and I've been using it for atleast 6 months :)
From emacswiki.org/emacs/RedoMode : However, redo.el is out-of-date and often destroys the contents of "buffer because the behavior of ‘primitive-undo’ has been slightly different from old one since Emacs 22. RedoPlus is a fork of redo.el and correctly works in newer version of Emacs. You can get RedoPlus here: emacswiki.org/emacs/redo+.el
I think, by default, this is now bound to M-_.
i
ideasman42

By default, redo in Emacs requires pressing C-g, then undo.

However it's possible use Emacs built-in undo to implement a redo command too.

The package undo-fu, uses Emacs built-in undo functionality to expose both undo and redo.

Edit, undo-fu has now been integrated into evil-mode (if you're an evil-mode user).

Example:

(use-package undo-fu)
(use-package evil
  :init
  (setq evil-undo-system 'undo-fu))

A
A.M.

Doom Emacs users, I hope you've scrolled this far or searched for 'doom' on the page...

Doom Emacs breaks the vanilla Emacs redo shortcut: C-g C-/ C-/ C-/ etc (or C-g C-_ C-_ C-_ etc) ...and instead that just keeps undoing.

Doom Emacs also breaks the undo-tree redo shortcut mentioned in one of the other answers as being useful for spacemacs etc: S-C-/ (AKA C-?) ...and instead that throws the error "C-? is not defined".

What you need is:

to be in evil-mode (C-z to toggle in and out of evil-mode) (in evil-mode should see blue cursor, not orange cursor) and

to be in 'command mode' AKA 'normal mode' (as opposed to 'insert mode') (Esc to switch to command mode) (should see block cursor, not line cursor), and then it's

u for undo and

C-r for redo


For non-evil mode users, look for the interactive function undo-fu-only-redo.
v
vineeshvs

If you want to redo the last operation, do the following.

ESC Do another undo (C + /)


B
Bhargav Das Gupta

In doom emacs,
Ctrl + Shift + - to undo
Alt + Shift + - to redo