Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 5: Recovering from Errors |  | Previous HourNext Hour
Sections in this Hour: |  |

## Recovering from a Crash

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.

### Recovering an Autosave File

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.

Figure 5.6
Emacs suggests recovering from the autosave file.

#### Recovering from an Autosave File

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.

Figure 5.7
Emacs asks for the file to recover.

  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.

Figure 5.8
Type the name to recover--without the # characters!

  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.

Figure 5.9
Emacs asks you a final time whether you want to recover from the autosave file.

Caution - If you start editing the file when Emacs has told you about the autosave file, you will be out of luck if you plan to recover from it. A new one will be written when you have typed 300 characters or have been idle for 30 seconds (whichever comes first!) If it was an accident that you started writing, hurry up and hit the Undo key, which will cancel this and let you do your recovering.

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.

### Recovering Sessions

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.

#### Recover Autosave Files

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.

Figure 5.10
Buffer with earlier sessions.

  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.

Figure 5.11
The contents of a session file.

  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.

Figure 5.12
Emacs asks you whether you want to recover the .emacs file.

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

Figure 5.13
Emacs asks a final time whether you want to recover a file.

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!

Tip - If you find that this is not the Seventh Wonder and you are actually quite annoyed with having the session files located in your home directory, you can disable the creation of these by inserting the following into your .emacs file: (setq auto-save-list-file-prefix nil) Although you have disabled this, you can still recover the files by using recover-file on each one of them.

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 5: Recovering from Errors |  | Previous HourNext Hour
Sections in this Hour: |  |

© Copyright Macmillan USA. All rights reserved.