Backups and auto-saving

Author

Marie-Hélène Burle

By default, Emacs has two mechanisms helping to prevent data loss: backups and auto-saving.

Backups

By default, Emacs creates a single backup file for each file (although, of course, this can be changed). Each time you re-open a file and make changes, the version prior to this current editing session gets saved as the backup file.

Backup files have names ending with ~.

Your turn:

Create a file called file.txt and add the following content in it:

This is my file.

Save it (C-x C-s).

Send Emacs to the background (C-z) and run ls in the terminal. You should see a file called file.txt.

cat file.txt shows you that it contains: This is my file.

Now, close, then re-open the file and make the following changes:

This is my file, but I have re-edited it.

Save it again.

Using ls and cat again (remember that you can also run Bash commands within Emacs with M-!), you should see that, in addition to your file file.txt with the content: This is my file, but I have re-edited it., there is now a backup file called file.txt~ with the content: This is my file.

As long as you don’t close the file, nothing happens to the backup file. But if you close it, re-open it, make new changes to it, and save it, the backup file.txt~ will now contain: This is my file, but I have re-edited it. (the version of the last editing session).

Backup files preserve files prior to the current editing session. This is useful if you make terrible mistakes during an editing session.

If you don’t like having backup files next to your files, you can hide them out of the way thanks to the variable backup-directory-alist which allows you to store backup files in a directory of your choice.

Also, remember that in Dired, you can remove all backup files by typing ~.

Auto-saving

Auto-saving is a much more familiar concept as many software do this. This ensures that you don’t lose too much work if your computer crashes. By default, this happens every 300 keystrokes or after 30 seconds of idle time. Now, what is unusual is that Emacs saves the file in a separate file so as not to touch the file you are working on (this can be very useful if saving a file triggers some costly process such as re-rendering a website or if you don’t want to save temporary sensitive information).

Auto-saved files are marked with # at the start and end of their names.

Your turn:

Edit file.txt and don’t save it for 300 keystrokes or idle for 30 seconds after some edits without saving. Now run ls and you will see that an auto-save file #file.txt# got created.

If you don’t like having auto-save files next to your files, you can hide them out of the way thanks to the variable auto-save-file-name-transforms which allows you to store backup files in a directory of your choice.

Also, remember that in Dired, you can remove all auto-save files by typing #.

Recovering data

If you run M-x recover-this-file from within file.txt, its content will be replaced by the content of #file.txt#. You can also do this from outside the file with M-x recover-file and then entering the file name.

If your system crashes while you have unsaved changes in a file, Emacs will offer you to recover the content of your file from its auto-saved version next time you open it.