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

## Automatic Backup

When you load an existing file into Emacs, it makes a backup of it for you the first time it saves this file, in case you later find that you need the original version.

Caution - Please note that it does not make a backup each time you save the buffer, but only when you save it the first time after you read it into Emacs.

There are two different ways Emacs can do these backups:

- Single backup--In this mode only one backup is kept of the file. When you edit the file for the second time in Emacs, the first backup is overwritten. The filename used for the backup is the original filename with a tilde (~) appended to the end of the file. (This is the default.)

- Numbered backup--In this mode, each new backup is numbered consecutively; thus, you might have several backups of the same file. The filename used for the backups is the original with .~42~ appended (for the 42nd backup of the file).

There are a number of variables that configure this feature. First of all, you can disable backup entirely. Please think carefully before you do this! To disable backup, insert the following line into your .emacs file:

    (setq make-backup-files nil)

If you would like Emacs to do a single backup (and it doesn't already), you must insert the following lines into your .emacs file:

     (setq make-backup-files t)
    (setq version-control 'never)

Finally, if you want Emacs to make numbered backups, you must insert the following lines into your .emacs file:

    (setq make-backup-files t)
    (setq version-control t)

### Configuring Numbered Backups

To avoid filling up your disk with backup files, Emacs only keeps a certain number of numbered backups. This number can be configured with two variables:

- kept-old-versions--This number of old versions is always kept. Thus if this variable is set to 3 then the first three numbered backups are never deleted. For example, file.~1~, file.~2~, file.~3~. As I see it, there are only two meaningful values to this variable, 0 and 1. 0 means do not keep a backup of the file from before you edit it the first time, and 1 means do. Having a value of 3, say, makes the two first edits more significant than the rest of them. I don't know why. The default value for this variable is 2.

- kept-new-versions--This number of backup files is kept, excluding the initial backups described by the variable. Thus if this variable is 2, and you are about to make backup number 20, all backup files except number 19 (that is file.~19~) and number 20 (that is file.~20~) are deleted (except, of course, the initial backup file described by the variable). The larger you set this variable, the more versions of files are stored on the disk and the more secure you are that you can find edits discarded by later versions of the file. This variable's default value is 2.

As an example, the following lines make Emacs keep the original file before you start to edit it and the five latest versions of it:

    (setq kept-old-versions 1)
    (setq kept-new-versions 5)

It should, of course, be inserted into your .emacs file along with the lines that enable numbered backups.

When old numbered backup files are about to be deleted, Emacs asks you for permission to do so. In the long run, this is very annoying, so when you have gained confidence in Emacs and its deletion of old backup files, you might want to insert the following into your .emacs file, which makes it silently delete the old backup files:

    (setq delete-old-versions t)

### Keeping All the Backup Files in One Directory

Backup files are normally written next to the files that they are a backup of. This has the advantage that they are easy to find, but it also has the disadvantage that they tend to clobber your hard disk. An alternative is to keep all your backup files in one directory.

To do this, you need the library called backup-dir. It is part of XEmacs, but GNU Emacs users need to copy it from the CD to their Lisp directory.

The library contains many different configurations. It is beyond the scope for this book to discuss all of them. (See the header of the Lisp file for the others.) One way to use it, however, is to make Emacs do all autosaving in one directory. To do this, insert lines similar to the following into your .emacs file:

    (require 'backup-dir)
    (setq bkup-backup-directory-info
          '((t "/home/blackie/.backups" ok-create full-path)))

You should of course replace /home/blackie/.backups with the directory in which you want your backup files to be located.

Caution - To use this library in GNU Emacs version 19, you need to install the customize library or the cust-stub library. See Appendix A, "Installing Functions and Packages from the CD," for instructions for installing these.

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.