tags:

views:

118

answers:

4

This article has the mention that "We say Emacs has redo because you can reverse direction while undoing, thereby undoing the undo.".

What does this mean? How can the user can 'redo' with emacs?

+8  A: 

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 E
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
+2  A: 

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-x z and then type many times on z key to repeat the command (interesting when you want to execute multiple times a macro)

Jérôme Radix
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
+2  A: 

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
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
I haven't had any problems yet, and I've been using it for atleast 6 months :)
monotux
+1  A: 

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
That looks really interesting. Cheers.
phils