Sams Teach Yourself Emacs in 24 Hours


Hour 5: Recovering from Errors

Previous HourNext Hour

Sections in this Hour:


Hour 5
Recovering from Errors

Emacs has several different facilities that help you avoid a disaster. These include

Undo and Redo

When you edit you sometimes get into a situation where you are unhappy about the latest changes. There might be several reasons for this:

Fortunately Emacs contains a very powerful Undo mechanism, which makes it possible for you to discard these changes. Unlike many other applications, Emacs keeps several steps of Undo available, not just one. To undo a command press C-_, or Control-underscore (undo). This undoes the latest command. If you press it once again, it undoes one step further back. You can continue this way until you get back to the beginning of the buffer or until you have hit the limit of the Undo commands (which is in the order of 20,000 character insertions). Any command other than the Undo command breaks the Undo chain.

Tip - If you use Emacs over a modem line or in another situation where C-_ doesn't work, use C-x u instead. This is, however, two key sequences, which means that it is very slow to use when undoing several steps. (That is, you have to press C-x u C-x u C-x u... instead of pressing C-_ several times.)

Caution - Not all commands add Undo information to the buffer, only those which actually change the buffer. Thus movement commands can not be undone, nor can the outline commands described in Hour 14, "Finding Differences Between Files."

Rather than having a Redo mechanism (that is, by default) the Undo commands of Emacs can themselves be undone. This might be very confusing to newcomers to Emacs but, if you get it right, it is in fact very simple: "Undo commands of Emacs are commands that later can be undone themselves."

An Undo Example

This task shows you an example of how the Undo commands work.

1. Insert the numbers 1, 2, 3, and 4 on separate lines, as shown in Figure 5.1. This is necessary because Emacs otherwise interprets the four numbers as one entity, causing them to be removed in one step by the Undo mechanism.

Figure 5.1
Initial layout before you start your Undo journey.

2. Now press Undo twice. This removes the 4 first, and 3 next, as shown in Figure 5.2.

Figure 5.2
Press Undo twice to remove 4 and then 3.

3. Now insert the number 5, as shown in Figure 5.3.

Figure 5.3
Insert 5. This breaks the Undo sequence and puts you in insert mode again (technically, there is no such thing as insert mode, but it might help think about it that way).

4. Now press Undo once. You can see, as shown in Figure 5.4, that 5 is once again removed. When you inserted 5, the Undo sequence was broken, and you now start to undo again.

Figure 5.4
Pressing Undo removes the latest text inserted, which is 5.

5. Continue pressing the Undo command. The first time 3 is inserted, the next time 4 is inserted, as shown in Figure 5.5. These are the numerals which were removed by the previous Undo commands.

Figure 5.5
Pressing Undo twice now inserts 3 and then 4.

6. If you continue pressing Undo, you see that first 4 is removed, then 3 is removed, then 2 is removed, and finally 1 is removed. Pressing Undo once more rings the bell, because you now are back at the initial state of the buffer. (This is, of course, true only if you did no editing in the buffer before you started this task.)

If you find this behavior strange, please think about what you would expect from the Undo mechanism. Should it reel back your changes as if you were recording them on video and reeling back the VCR? Okay, then please look through these steps again; you will, in fact, see that this is exactly what is happening! It includes you reeling back during your first Undo session.

Tip - If you accidentally press a key, and Emacs does some fancy thing and you simply must know what is, try pressing C-h l (view-lossage). This splits your window in two and shows you what keys you have pressed. To make your buffer take up all Emacs's space again, press C-x 1 (delete-other-windows).

The Redo Library

Having only the Undo command is a drawback if you need to get back to a state you had some time ago. An example might be that you make changes and, five minutes later, you find that this is a bad idea after all. Emacs contains enough Undo information to get back to the state before you started your changes. The problem, however, is to find the exact location if you made changes before you started the current editing (that is, you didn't load the file into Emacs and started editing on the change you now regret, but you had already made some changes).

If you undo only a few commands and break the Undo sequence (for example, by inserting a space), you can't Undo anything else past that because you have to undo your Undo before you can undo the real typing. Think about it!

Okay, what you really want is a Redo mechanism which simply reverses an Undo when you are undoing so you can reel forth and back around the point where the changes started, and thus find the exact point.

This feature is available in the library called Redo. It is included in XEmacs but not in GNU Emacs.

If you are using GNU Emacs, you can copy the library from the CD that comes with book to your Lisp directory.

To install it, simply add the following into your .emacs file:

(require 'redo)

This makes the command redo available, but you should also bind it to a key. You might, for example, want to bind Undo to F5 and Redo to Shift+F5, which can be done by inserting the following lines into your .emacs file:

(global-set-key [(f5)] 'undo) 

Tip - (global-set-key [(shift f5)] 'redo) Adding this library does not alter the way ordinary Undo works; it simply causes Emacs to start a bit more slowly.

Reverting to the File on Disk

If you regret everything you have done since you last saved your file, you can ask Emacs to reload it from disk for you by pressing M-x and typing revert-buffer. Be careful; this deletes the changes you made to the buffer forever!

Tip - This might also be used if your file changed on disk since you loaded it or saved it the last time. A log file might be an example of this.

Sams Teach Yourself Emacs in 24 Hours


Hour 5: Recovering from Errors

Previous HourNext Hour

Sections in this Hour: