Emacs: Real Automatic Save File

By Xah Lee. Date: . Last updated: .

Real Auto Save File

Make emacs automatically save file. New in Emacs 26 (Released 2018-06)

Put this in your Emacs Init File:

(when (>= emacs-major-version 26)
  ;; real auto save
  (auto-save-visited-mode 1)
  (setq auto-save-visited-interval 30))

Auto Save File When Switching Out of Emacs

This is the best solution i use since about 2017. Most simple code, reliable, and practical.

Save all files when you switch out of emacs.

Put this in your Emacs Init File:

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

(if (version< emacs-version "27.1")
    (add-hook 'focus-out-hook 'xah-save-all-unsaved)
  (setq after-focus-change-function 'xah-save-all-unsaved))
;; to undo this, run
;; (setq after-focus-change-function 'ignore)

Reference

Emacs: auto save, backup