Sams Teach Yourself Emacs in 24 Hours |
||||||||||||||
Hour 5: Recovering from Errors |
||||||||||||||
|
In the previous section, you saw that Emacs makes a backup of your files when it writes a new version of them the first time. This enables you to revert to an old version in case you decide to discard the changes made. This does not, however, save you if you edit a document for several hours without saving once in awhile, and Emacs or your system crashes, or you leave your window manager without asking Emacs to save.
I strongly suggest that you save your files often, which would make the amount of data lost in this case smaller. But fortunately Emacs helps you here too. Emacs saves your files once in a while: normally, every 300 keystrokes or when you have been inactive for 30 seconds.
Caution - Please note that only buffers that have been loaded from a file or written to a file are saved. Thus you should always start writing a new file by opening it with C-x C-f (find-file). An alternative that you should not use is to write in the *scratch* buffer, and then write this to a file with C-x C-w (write-file). |
When writing the backup files, Emacs uses a special filename, namely the original name with a hash mark, or pound symbol, (#) in front and in back of it. Thus the file index.html will be autosaved to the file #index.html#. It does not save to the original filename for a couple of reasons:
If Emacs or your computer crashes while Emacs is writing the backup file, or if there is no more space left on your hard drive, all will be lost, and all you have to hope for is the backup file made when you first saved the file (which might have been several days ago).
If you are editing a live file (one that can be read when you are editing it, such as the password file) then the consistency of the file might be lost. (If there is an error in the password file, people cannot log in to the system!)
When Emacs does autosaving, it might freeze for a few seconds (depending of the size of the file), but you can still type; it shows up a bit later on your screen. To indicate to you that it is autosaving, it shows the text Auto-saving... in the minibuffer.
When you save the buffer, Emacs removes the autosave file; thus when you exit Emacs with all your buffers saved, there should be no autosave files left on the hard drive.
When you load a file with an autosave file into Emacs, it tells you this and suggests that you recover this file, as you can see in Figure 5.6.
Follow these steps to recover from an autosave file:
1. Press M-x and type recover-file. Emacs now asks you for the filename to recover (that is, the name without the # characters), as shown in Figure 5.7.
2. Type the name and press Enter, as shown in Figure 5.8. Remember you can press the Tab key here to complete the filename.
3. As shown in Figure 5.9, Emacs shows you the size and the date of the original file and the autosave file to make sure that you know what you are doing. To confirm that you are sane and ready to recover the file, type yes.
This procedure is
no different from copying the autosave file to the original file, and then starting
Emacs with this file. But I hope you find it a bit easier.
If you had several files loaded and changed at the time Emacs crashed, you might have several autosave files to recover. Emacs makes this a bit easier for you by creating a list of all these files in a file within your home directory. This file is called .saves-pid-hostname, where pid is the process identifier of your Emacs ( pid is a number; if you don't know what it is, don't bother), and hostname is the name of the machine running the Emacs. Rather than invoking recover-file you can use the function recover-session.
Follow these steps to use recover-session to recover multiple autosave files in one go:
1. Press M-x and type recover-session. Emacs now shows you a list of all the session files, sorted by date, as shown in Figure 5.10.
2. In this buffer, you can press Enter on top of a session to see which files it contains, as shown in Figure 5.11.
3. To get back to the session buffer, simply kill the buffer with the information about the session. You do this by pressing C-x k (kill-buffer) and pressing the Enter key.
4. Now you should be back with the session list, as shown in Figure 5.12. Press C-c C-c.on top of the session you want to recover.
5. Emacs now asks you whether it should recover the first file of the session. You have the following choices:
y
Press y to answer Yes, recover this file.
n
Press n to answer No, skip this file and continue with the other files.
!
Press the exclamation mark to make Emacs recover all the files of the session without asking you for each of them.
q
Press q to ask Emacs to quit recovering files.
.
Press the period to ask Emacs to recover this file but skip recovering the others.
Finally, if you forget what the different bindings mean, press C-h to ask Emacs for a description of them.
After you give Emacs permission to recover a file from the session, it asks you again to make sure you know what you are doing (see Figure 5.13).
When you have answered all of Emacs's questions, the files
are read into buffers and are
ready to edit, so please do not forget to save them!
Sams Teach Yourself Emacs in 24 Hours |
||||||||||||||
Hour 5: Recovering from Errors |
||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.