Skip to main content

18.3.2 Backup Files

On most operating systems, rewriting a file automatically destroys all record of what the file used to contain. Thus, saving a file from Emacs throws away the old contents of the file—or it would, except that Emacs carefully copies the old contents to another file, called the backup file, before actually saving.

Emacs makes a backup for a file only the first time the file is saved from a buffer. No matter how many times you subsequently save the file, its backup remains unchanged. However, if you kill the buffer and then visit the file again, a new backup file will be made.

For most files, the variable make-backup-files determines whether to make backup files. On most operating systems, its default value is t, so that Emacs does write backup files.

For files managed by a version control system (see Version Control), the variable vc-make-backup-files determines whether to make backup files. By default it is nil, since backup files are redundant when you store all the previous versions in a version control system. See General VC Options.

At your option, Emacs can keep either a single backup for each file, or make a series of numbered backup files for each file that you edit. See Backup Names.

The default value of the backup-enable-predicate variable prevents backup files being written for files in the directories used for temporary files, specified by temporary-file-directory or small-temporary-file-directory.

You can explicitly tell Emacs to make another backup file from a buffer, even though that buffer has been saved before. If you save the buffer with C-u C-x C-s, the version thus saved will be made into a backup file if you save the buffer again. C-u C-u C-x C-s saves the buffer, but first makes the previous file contents into a new backup file. C-u C-u C-u C-x C-s does both things: it makes a backup from the previous contents, and arranges to make another from the newly saved contents if you save again.

You can customize the variable backup-directory-alist to specify that files matching certain patterns should be backed up in specific directories. A typical use is to add an element ("." . dir) to make all backups in the directory with absolute name dir. Emacs modifies the backup file names to avoid clashes between files with the same names originating in different directories. Alternatively, adding, ("." . ".~") would make backups in the invisible subdirectory .~ of the original file’s directory. Emacs creates the directory, if necessary, to make the backup.

Names  How backup files are named.
Deletion  Emacs deletes excess numbered backups.
Copying  Backups can be made by copying or renaming.

18.3.2.1 Single or Numbered Backups

When Emacs makes a backup file, its name is normally constructed by appending ‘~’ to the file name being edited; thus, the backup file for eval.c would be eval.c~.

If access control stops Emacs from writing backup files under the usual names, it writes the backup file as ~/.emacs.d/%backup%~. Only one such file can exist, so only the most recently made such backup is available.

Emacs can also make numbered backup files. Numbered backup file names contain ‘.~’, the number, and another ‘~’ after the original file name. Thus, the backup files of eval.c would be called eval.c.~1~, eval.c.~2~, and so on, all the way through names like eval.c.~259~ and beyond.

The variable version-control determines whether to make single backup files or multiple numbered backup files. Its possible values are:

nil

Make numbered backups for files that have numbered backups already. Otherwise, make single backups. This is the default.

t

Make numbered backups.

never

Never make numbered backups; always make single backups.

The usual way to set this variable is globally, through your init file or the customization buffer. However, you can set version-control locally in an individual buffer to control the making of backups for that buffer’s file (see Locals). You can have Emacs set version-control locally whenever you visit a given file (see File Variables). Some modes, such as Rmail mode, set this variable.

If you set the environment variable VERSION_CONTROL, to tell various GNU utilities what to do with backup files, Emacs also obeys the environment variable by setting the Lisp variable version-control accordingly at startup. If the environment variable’s value is ‘t’ or ‘numbered’, then version-control becomes t; if the value is ‘nil’ or ‘existing’, then version-control becomes nil; if it is ‘never’ or ‘simple’, then version-control becomes never.

If you set the variable make-backup-file-name-function to a suitable Lisp function, you can override the usual way Emacs constructs backup file names.

18.3.2.2 Automatic Deletion of Backups

To prevent excessive consumption of disk space, Emacs can delete numbered backup versions automatically. Generally Emacs keeps the first few backups and the latest few backups, deleting any in between. This happens every time a new backup is made.

The two variables kept-old-versions and kept-new-versions control this deletion. Their values are, respectively, the number of oldest (lowest-numbered) backups to keep and the number of newest (highest-numbered) ones to keep, each time a new backup is made. The backups in the middle (excluding those oldest and newest) are the excess middle versions—those backups are deleted. These variables’ values are used when it is time to delete excess versions, just after a new backup version is made; the newly made backup is included in the count in kept-new-versions. By default, both variables are 2.

If delete-old-versions is t, Emacs deletes the excess backup files silently. If it is nil, the default, Emacs asks you whether it should delete the excess backup versions. If it has any other value, then Emacs never automatically deletes backups.

Dired’s . (Period) command can also be used to delete old versions. See Flagging Many Files.

18.3.2.3 Copying vs. Renaming

Backup files can be made by copying the old file or by renaming it. This makes a difference when the old file has multiple names (hard links). If the old file is renamed into the backup file, then the alternate names become names for the backup file. If the old file is copied instead, then the alternate names remain names for the file that you are editing, and the contents accessed by those names will be the new contents.

The method of making a backup file may also affect the file’s owner and group. If copying is used, these do not change. If renaming is used, you become the file’s owner, and the file’s group becomes the default (different operating systems have different defaults for the group).

The choice of renaming or copying is made as follows:

  • If the variable backup-by-copying is non-nil (the default is nil), use copying.

  • Otherwise, if the variable backup-by-copying-when-linked is non-nil (the default is nil), and the file has multiple names, use copying.

  • Otherwise, if the variable backup-by-copying-when-mismatch is non-nil (the default is t), and renaming would change the file’s owner or group, use copying.

    If you change backup-by-copying-when-mismatch to nil, Emacs checks the numeric user-id of the file’s owner and the numeric group-id of the file’s group. If either is no greater than backup-by-copying-when-privileged-mismatch, then it behaves as though backup-by-copying-when-mismatch is non-nil anyway.

  • Otherwise, renaming is the default choice.

When a file is managed with a version control system (see Version Control), Emacs does not normally make backups in the usual way for that file. But committing (a.k.a. checking in, see VCS Concepts) new versions of files is similar in some ways to making backups. One unfortunate similarity is that these operations typically break hard links, disconnecting the file name you visited from any alternate names for the same file. This has nothing to do with Emacs—the version control system does it.