By default, Emacs automatically saves your changes to a file intermittently. If anything should happen, you can recover a file with ‘M-x recover-file’. Auto-saving can be turned on globally or on a per-buffer basis with ‘M-x auto-save-mode’ (users of Emacs 26.1 and later may prefer ‘M-x auto-save-visited-mode’). Note that auto-saving and backing up are different concepts and serve different purposes.

See also the Auto Save Emacs manual entry.

Placing all files in one directory

By default, auto-save files are stored in the current directory with a file name on the form #file#. If you don’t want to clutter up your file tree with Emacs’ backup files, you can save them to a dedicated directory:

    (setq backup-directory-alist
          `(("." . ,(concat user-emacs-directory "backups"))))

Saving to the system’s “temp” directory is also an option, though a dangerous one, since in regular Linux setups there’s no guarantee that you will see those backups again:

    (setq backup-directory-alist
          `((".*" . ,temporary-file-directory)))
    (setq auto-save-file-name-transforms
          `((".*" ,temporary-file-directory t)))

This will place all auto-saves and backups in the directory pointed to by temporary-file-directory (e.g., C:/Temp/ on Windows, /tmp/ on Linux).

Or even more dramatic:

    ;; Save all tempfiles in $TMPDIR/emacs$UID/                                                        
    (defconst emacs-tmp-dir (expand-file-name (format "emacs%d" (user-uid)) temporary-file-directory))
    (setq backup-directory-alist
        `((".*" . ,emacs-tmp-dir)))
    (setq auto-save-file-name-transforms
        `((".*" ,emacs-tmp-dir t)))
    (setq auto-save-list-file-prefix

Delete auto-save files

    find -name "#*#" -print -delete

Interlock symlinks and auto-save files

Be aware that symbolic links of the form “.#*” are not auto-save files but interlocks to prevent the simultaneous editing of the same file. See Interlocking in the EmacsManual for details.

Running save-buffer Automatically

For Emacs 26.1 and later:

As written in Auto Save Files:

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.

For older versions of Emacs, or for greater flexibility you can use a 3rd-party package:

After Some Time Interval via Idle Timers - Automatically save your all your buffers/files at regular intervals in Emacs!

Save All Buffers on Auto-Save

I like using ‘auto-save-hook’, and that seems to work well, but I want to save all the buffers, because if I switch away from a buffer before it autosaves, I want it to get saved when things next get saved after that. So I combined parts of the two above solutions (this is the first elisp code I’ve written, could have glaring problems):

(defun full-auto-save ()
    (dolist (buf (buffer-list))
      (set-buffer buf)
      (if (and (buffer-file-name) (buffer-modified-p))
(add-hook 'auto-save-hook 'full-auto-save)

On Loss of Input Focus via Focus Hooks

New hooks for gain/loss of focus were added in Gnu Emacs 24.4: focus-in-hook and focus-out-hook.

If you are running a version of Emacs with these hooks, you can add

  (defun save-all ()
    (save-some-buffers t))
  (add-hook 'focus-out-hook 'save-all)

to your .emacs file and it should save all files on loss of focus.

Two things to note:

These hooks fire when any Emacs frame gains or loses focus. This means that if you Alt-Tab or Alt-` between Emacs frames, the focus-out-hook and focus-in-hook will run. (MacOS and Windows have a concept of application-level focus, but X apparently does not. These hooks apply at the frame level so they can work on all three platforms.)

These hooks are intended for use by the end-user, not for use in packages. If you set these hooks for other people, they’re likely to be annoyed. (Particularly if they are debugging Emacs itself or working in a “focus follows mouse” environment where merely mousing over a window switches focus to it.)

On User-Defined Signals

Another approach is tell emacs to do ‘full-auto-save’ (defined above) when it receives the USR1 signal.

  (define-key special-event-map [sigusr1] #'full-auto-save)

Create a shell script to send the signal to emacs.

  #!/usr/bin/env sh
  pkill -USR1 emacs

And tell your window manager to run the script whenever you switch windows. I use sawfish and so:

  ;;.sawfish/rc code
  (defun save-emacs () 
    (system "save-emacs &"))
  (add-hook 'leave-workspace-hook save-emacs)