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?
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?
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:
Now, you undo. It undoes the last action, resulting in the following list:
If you do something other than undo at this point - say, C-f, the operation stack looks like this:
Now, when you undo, the first thing that is undone is the undo. Resulting in your original stack (and document state):
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
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.
undo-redo
answer on this page). –
Iiette 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)
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. –
Blindly C-x e
), you can just press e
as often as you need to execute it again. –
Chalcography C-/
and C-x u
are alternative undo key bindings, equivalent to C-_
. I prefer C-/
since it's easier to hit. –
Avert 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... –
Avert M-x undo-only
does what you’re thinking of. But C-_
triggers M-x undo
, and that always undoes something, in one direction or the other – it doesn’t care whether what it’s undoing is another undo. See Undo in the Emacs Manual for more details. –
Avert C-/
C-/
C-/
C-g
C-/
C-g
C-/
C-/
. Note that C-g
is not repeated.C-g
C-/
C-g
C-/
C-/
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.
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.
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.
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).
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
.
M-_
. –
Marder 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))
Doom Emacs users, I hope you've scrolled this far or searched for 'doom' on the page...
C-g
C-/
C-/
C-/
etc (or C-g
C-_
C-_
C-_
etc)
...and instead that just keeps undoing.S-C-/
(AKA C-?
)
...and instead that throws the error "C-? is not defined".What you need is:
C-z
to toggle in and out of evil-mode) (in evil-mode should see blue cursor, not orange cursor) andEsc
to switch to command mode) (should see block cursor, not line cursor), and then it'su
for undo andC-r
for redoundo-fu-only-redo
. –
Fujio If you want to redo the last operation, do the following.
ESC
C + /
)In doom emacs,
Ctrl + Shift + -
to undo
Alt + Shift + -
to redo
© 2022 - 2024 — McMap. All rights reserved.