Skip to main content

18.6.1 Auto-Save Files

Auto-saving does not normally save in the files that you visited, because it can be very undesirable to save a change that you did not want to make permanent. Instead, auto-saving is done in a different file called the auto-save file, and the visited file is changed only when you request saving explicitly (such as with C-x C-s).

Normally, the auto-save file name is made by appending ‘#’ to the front and rear of the visited file name. Thus, a buffer visiting file foo.c is auto-saved in a file #foo.c#. Most buffers that are not visiting files are auto-saved only if you request it explicitly; when they are auto-saved, the auto-save file name is made by appending ‘#’ to the front and rear of buffer name, then adding digits and letters at the end for uniqueness. For example, the *mail* buffer in which you compose messages to be sent might be auto-saved in a file named #*mail*#704juu. Auto-save file names are made this way unless you reprogram parts of Emacs to do something different (the functions make-auto-save-file-name and auto-save-file-name-p). The file name to be used for auto-saving in a buffer is calculated when auto-saving is turned on in that buffer.

The variable auto-save-file-name-transforms allows a degree of control over the auto-save file name. It lets you specify a series of regular expressions and replacements to transform the auto save file name. The default value puts the auto-save files for remote files (see Remote Files) into the temporary file directory on the local machine.

When you delete a substantial part of the text in a large buffer, auto save turns off temporarily in that buffer. This is because if you deleted the text unintentionally, you might find the auto-save file more useful if it contains the deleted text. To reenable auto-saving after this happens, save the buffer with C-x C-s, or use C-u 1 M-x auto-save-mode.

If you want auto-saving to be done in the visited file rather than in a separate auto-save file, enable the global minor mode auto-save-visited-mode. In this mode, auto-saving is identical to explicit saving. Note that this mode is orthogonal to the auto-save mode described above; you can enable both at the same time. However, if auto-save mode is active in some buffer and the obsolete auto-save-visited-file-name variable is set to a non-nil value, that buffer won’t be affected by auto-save-visited-mode.

You can use the variable auto-save-visited-interval to customize the interval between auto-save operations in auto-save-visited-mode; by default it’s five seconds. auto-save-interval and auto-save-timeout have no effect on auto-save-visited-mode. See Auto Save Control, for details on these variables.

A buffer’s auto-save file is deleted when you save the buffer in its visited file. (You can inhibit this by setting the variable delete-auto-save-files to nil.) Changing the visited file name with C-x C-w or set-visited-file-name renames any auto-save file to go with the new visited name.