300

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?

phils
  • 71,335
  • 11
  • 153
  • 198
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 4
    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. – Russia Must Remove Putin Nov 02 '14 at 18:19
  • 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. – nroose Oct 22 '16 at 16:16
  • 1
    Emacs 28 now supports this, see: https://stackoverflow.com/a/60163018/432509 – ideasman42 Mar 17 '21 at 22:45

11 Answers11

322

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.

Michael Ekstrand
  • 28,379
  • 9
  • 61
  • 93
  • 133
    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. – phils Aug 20 '10 at 00:15
  • 3
    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 Aug 21 '13 at 11:31
  • Do you know for sure that the Emacs operation stack only has the undos added, after the undo sequence is broken? – barlop Aug 23 '13 at 11:44
  • 1
    @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. – Michael Ekstrand Jun 07 '14 at 16:52
  • @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? – barlop Jun 07 '14 at 17:20
  • @barlop 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. – Michael Ekstrand Jun 08 '14 at 00:25
  • @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? – barlop Jun 08 '14 at 01:26
  • 20
    I don't know why this answer was accepted when it doesn't even tell you *HOW*. – Russia Must Remove Putin Nov 02 '14 at 18:12
  • 17
    Sadly this answer doesn't say anything about the redo command which is C-g C-/. – vishless Dec 01 '17 at 06:47
  • 1
    This answer is outdated as of Emacs 28 (see `undo-redo` answer on this page). – ideasman42 Feb 16 '20 at 07:53
238

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)

programking
  • 1,376
  • 1
  • 17
  • 32
Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40
  • 8
    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. – Patrick Aug 20 '10 at 10:13
  • 11
    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. – Christian Berg Nov 10 '11 at 08:51
  • 7
    Also, `C-/` and `C-x u` are alternative undo key bindings, equivalent to `C-_`. I prefer `C-/` since it's easier to hit. – Rory O'Kane Apr 25 '13 at 21:36
  • 12
    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... – Rory O'Kane Apr 25 '13 at 21:41
  • @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. – barlop Aug 21 '13 at 02:49
  • @barlop 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](http://www.gnu.org/software/emacs/manual/html_node/emacs/Undo.html) in the Emacs Manual for more details. – Rory O'Kane Aug 22 '13 at 14:43
  • apropos of nothing, neither of the standard undo keystrokes (C-_, C-/) work on the android platform: the control seems to get stripped off. – Michael Feb 20 '14 at 17:01
  • Sorry but this simply does not work. It always end in "no further undo information" –  Oct 22 '15 at 20:54
  • what is C-_ called so I can rebind it? – christopher clark Nov 05 '20 at 00:23
75
  • 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.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
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.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 6
    Fantastic! Who said emacs undo was unfathomable? :-) – Peter.O Oct 27 '11 at 06:36
  • 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. – Basel Shishani Dec 03 '14 at 00:29
  • 1
    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. – Basel Shishani Dec 03 '14 at 00:58
  • 1
    Beware this has a long standing bug in redo which can randomly fail: https://github.com/syl20bnr/spacemacs/issues/9903 - known about since 2014 – ideasman42 Dec 04 '17 at 02:06
  • @ideasman42 Apparently this is a "non-bug": http://www.dr-qubit.org/Lost_undo-tree_history.html – Dalker Feb 03 '20 at 06:18
  • 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. – ideasman42 Feb 07 '20 at 01:01
  • @ideasman42 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. – Trey Jackson Feb 08 '20 at 03:29
  • 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. – ideasman42 Feb 08 '20 at 06:58
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.

Micah Elliott
  • 9,600
  • 5
  • 51
  • 54
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).

ideasman42
  • 42,413
  • 44
  • 197
  • 320
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.

monotux
  • 3,657
  • 28
  • 30
  • 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? – A. Levy Aug 20 '10 at 15:05
  • I haven't had any problems yet, and I've been using it for atleast 6 months :) – monotux Aug 20 '10 at 17:43
  • 5
    From http://www.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: http://www.emacswiki.org/emacs/redo+.el – rofrol Jan 23 '14 at 16:46
  • I think, by default, this is now bound to `M-_`. – supercheetah Apr 04 '22 at 03:07
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))
ideasman42
  • 42,413
  • 44
  • 197
  • 320
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
A.M.
  • 627
  • 2
  • 8
  • 18
2

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

  1. ESC
  2. Do another undo (C + /)
vineeshvs
  • 479
  • 7
  • 32
0

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