Sams Teach Yourself Emacs in 24 Hours |
||||||||||||||
Hour 5: Recovering from Errors |
||||||||||||||
|
Emacs has several different facilities that help you avoid a disaster. These include
Undo commands, which let you get back to the state of your document before your most-recent edits.
Automatic backup, which makes a backup copy of your files when you start to edit them
Autosave, which saves your buffer to a temporary file within a given interval. This should, you can hope, lessen the catastrophe if you forget to save the content of your buffer when leaving Emacs or in case either Emacs or your computer crashes.
When you edit you sometimes get into a situation where you are unhappy about the latest changes. There might be several reasons for this:
You might regret the edits you have done.
You might, by accident, execute a macro or press a key that makes unexpected changes to your buffer.
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.
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."
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.
2. Now press Undo twice. This removes the 4 first, and 3 next, as shown in Figure 5.2.
3. Now insert the number 5, as shown in Figure 5.3.
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.
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.
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.
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. |
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 |
||||||||||||||
|
© Copyright Macmillan USA. All right