How do you 'redo' changes after 'undo' with Emacs?
Asked Answered
P

11

304

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?

Poteet answered 19/8, 2010 at 23:41 Comment(3)
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.Simonnesimonpure
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.Dethrone
Emacs 28 now supports this, see: https://mcmap.net/q/99548/-how-do-you-39-redo-39-changes-after-39-undo-39-with-emacsIiette
B
327

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:

  1. Insert "foo"
  2. Insert "bar"
  3. Insert "I love spam"

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

  1. Insert "foo"
  2. Insert "bar"

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

  1. Insert "foo"
  2. Insert "bar"
  3. Insert "I love spam"
  4. 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):

  1. Insert "foo"
  2. Insert "bar"
  3. 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

  1. Insert "foo"
  2. Insert "bar"
  3. Insert "I love spam"
  4. Undo insert "I love spam"
  5. 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.

Bk answered 19/8, 2010 at 23:50 Comment(10)
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.Goth
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 fromHelenehelenka
Do you know for sure that the Emacs operation stack only has the undos added, after the undo sequence is broken?Helenehelenka
@Helenehelenka 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.Bk
@MichaelEkstrand i'm not sure if stacks are user visible or to what extent if they are.You maybe assume not..let's assume not,but what you call a "user-visible perspective" is actually a hypothetical implementation, and it's possible another hypothetical implementation(eg with one stack and a pointer) could be simpler. Does your hypothetical implementation involve two stacks? for example, you say that when the stack has two items "insert foo" and "insert bar" and he does C-f,then another two items go in there.You haven't shown where they came from,perhaps another stack that you didn't show?Helenehelenka
@Helenehelenka I do not know how Emacs' internal implementation works. I have edited my answer slightly to try to make it clearer that I am using stacks as a conceptual tool to explain how to think about and use Emacs' undo/redo capabilities.Bk
@MichaelEkstrand ok, and I understand that. But what I asked you was querying your conceptual construct. you say that when the stack has two items "insert foo" and "insert bar" and he does C-f,then another two items go in there.You haven't shown -within your concept- where they came from,perhaps another stack -in your concept of it- that you didn't show?Helenehelenka
I don't know why this answer was accepted when it doesn't even tell you HOW.Simonnesimonpure
Sadly this answer doesn't say anything about the redo command which is C-g C-/.Iodoform
This answer is outdated as of Emacs 28 (see undo-redo answer on this page).Iiette
I
240

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)

Illegalize answered 20/8, 2010 at 8:19 Comment(9)
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.Blindly
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.Chalcography
Also, C-/ and C-x u are alternative undo key bindings, equivalent to C-_. I prefer C-/ since it's easier to hit.Avert
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...Avert
@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.Helenehelenka
@Helenehelenka A good guess, but no, Emacs’s undo doesn’t work that way. The command 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
apropos of nothing, neither of the standard undo keystrokes (C-_, C-/) work on the android platform: the control seems to get stripped off.Norwich
Sorry but this simply does not work. It always end in "no further undo information"Cuddy
what is C-_ called so I can rebind it?Kristianson
B
76
  • 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.

Bugeye answered 22/8, 2013 at 14:34 Comment(0)
U
44

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.

Underhanded answered 20/8, 2010 at 15:42 Comment(8)
Fantastic! Who said emacs undo was unfathomable? :-)Enuresis
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.Denaturalize
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.Denaturalize
Beware this has a long standing bug in redo which can randomly fail: github.com/syl20bnr/spacemacs/issues/9903 - known about since 2014Iiette
@Iiette Apparently this is a "non-bug": dr-qubit.org/Lost_undo-tree_history.htmlIntitule
It may be a non-bug technically speaking. From a user perspective you loose your undo history randomly - it might as well be a bug.Iiette
@Iiette I hear you, and there are "fixes" for that. Emacs was designed by and for people (who became) power users. The maintainers chose not to dumb down the tool for the common denominator, for better or worse.Underhanded
I tried all the fixes people mentioned but still ran into problems, so ended up writing my own undo/redo which is significantly simpler than undo-tree, called "undo-fu" linked on this page. So far I've had a positive response from users.Iiette
W
26

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.

Whited answered 14/3, 2016 at 18:50 Comment(1)
This. I am using Spacemacs and couldn't figure out why it wasn't working for me. Thanks!Colossae
I
14

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).

Iiette answered 11/2, 2020 at 6:19 Comment(0)
E
11

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.

Excursus answered 20/8, 2010 at 13:14 Comment(4)
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?Apyretic
I haven't had any problems yet, and I've been using it for atleast 6 months :)Excursus
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+.elBirdiebirdlike
I think, by default, this is now bound to M-_.Marder
I
4

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))
Iiette answered 14/12, 2019 at 5:59 Comment(0)
S
3

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
Sis answered 7/1, 2021 at 5:32 Comment(1)
For non-evil mode users, look for the interactive function undo-fu-only-redo.Fujio
C
2

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

  1. ESC
  2. Do another undo (C + /)
Coan answered 13/4, 2019 at 7:37 Comment(0)
D
0

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

Damron answered 2/7, 2022 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.