emacsundo-redo

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?


Solution

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