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

EmacsUndo Redo

Emacs Problem Overview


http://www2.lib.uchicago.edu/keith/tcl-course/emacs-tutorial.html">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?

Emacs Solutions


Solution 1 - 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:

  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.

Solution 2 - Emacs

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)

Solution 3 - Emacs

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

Solution 4 - Emacs

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.

Solution 5 - Emacs

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.

Solution 6 - Emacs

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

Solution 7 - Emacs

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.

Solution 8 - Emacs

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

Solution 9 - Emacs

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

Solution 10 - Emacs

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

  1. ESC
  2. Do another undo (C + /)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprosseekView Question on Stackoverflow
Solution 1 - EmacsMichael EkstrandView Answer on Stackoverflow
Solution 2 - EmacsJérôme RadixView Answer on Stackoverflow
Solution 3 - EmacsRory O'KaneView Answer on Stackoverflow
Solution 4 - EmacsTrey JacksonView Answer on Stackoverflow
Solution 5 - EmacsMicah ElliottView Answer on Stackoverflow
Solution 6 - Emacsideasman42View Answer on Stackoverflow
Solution 7 - EmacsmonotuxView Answer on Stackoverflow
Solution 8 - Emacsideasman42View Answer on Stackoverflow
Solution 9 - EmacsA.M.View Answer on Stackoverflow
Solution 10 - EmacsvineeshvsView Answer on Stackoverflow