Emacs: Auto Save

By Xah Lee. Date: . Last updated: .

Real Auto Save File

put this in emacs init.

(defun xah-save-all-unsaved ()
  "Save all unsaved files. no ask.
Version 2019-11-05"
  (save-some-buffers t ))

;; when switching out of emacs, all unsaved files will be saved
(add-hook 'focus-out-hook 'xah-save-all-unsaved)

disable emacs's “#auto-save#” backup

;; stop creating those #auto-save# files
(setq auto-save-default nil)

emacs has auto-save-mode, however, it's not what you think.

Emacs's auto-save-mode periodically saves a copy of your file with the name #filename#.

When you save the file, those #files# are deleted automatically.

In case of crash or electricity outage, when you open a file afterward, emacs will detect those #files# and ask if you want to recover.

But the problem is, even if no crash, in lots of situations (such as sync to server), you'll find those #files# annoying, because they are often symbolic links to non-existent files, and will cause tools like rsync to complain or choke some dir walk scripts.

See also: Emacs: Turn Off Backup

stop emacs from creating .#lock file links

emacs backup problem 2018-10-12 58cc4
emacs lock file problem 2018-10-12

emacs backup is so annoying. even when you have it off (setq auto-save-default nil) , it still creates temp # files. which crashes script that traverse dir.


(setq create-lockfiles nil)

This will completely stop emacs from creating temoporary symbolic link file named “#something”.

(disable lock file may be a problem if you have situations where a file is being edited by different people or instances of emacs.)

2018-10-12 thanks to Dale Hagglund [ https://twitter.com/DaleHagglund/status/1050786629844004865 accessed: 2018-10-12] for create-lockfiles

alternative solution and problem

You can set emacs to auto save files directly, instead of creating those #filename#.

Put the following in your emacs init:

(setq auto-save-default t)
(setq auto-save-visited-file-name t)


However, this does not work well. Emacs still generates those #files#, this time, named like this:

.#xxtemp.201601134145.44920.el -> xah@xah-p6813w.21261:1452244010

If you have scripts going thru your machine, you get this error:

error: cannot open /home/john/web/.#vocabulary.html No such file or directory

(info "(elisp) Auto-Saving")

There are probably ways to fix this, with hooks or timer, but i haven't looked into.

thanks to xfq [https://github.com/xfq/] and finalpatch [http://www.weibo.com/u/1885517634]

thanks to http://irreal.org/blog/?p=4867, Anna Pawlicka (@AnnaPawlicka)

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

Emacs Init

Init Basics


text editing