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"
  (interactive)
  (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.

Solution:

(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)

Problems

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)

Emacs Customization

  1. Emacs init file
  2. What's Major Mode
  3. What's Minor Mode
  4. M-x customize
  5. Organize Init File
  6. Byte Compile Elisp
  7. What's Hook
  8. Avoid Lambda in Hook
  9. Environment Variables in Emacs
  10. Check OS, Version, Host Name
  11. Check Defined/Loaded

packages

  1. Install Packages
  2. Install Package Manually

text editing

  1. Define Keys
  2. Set Mouse Buttons
  3. Tabs, Space, Indentation Setup
  4. auto bracket pair
  5. Copy/Cut Line If No Selection
  6. Isearch space for - _
  7. Ido Completion
  8. Icomplete Completion
  9. Move Cursor by camelCase
  10. Stop Cursor Going into Minibuffer Prompt
  11. Sync Clipboard with Linux X11

file

  1. Default Major Mode
  2. Auto Backup Off
  3. Auto Save
  4. Restore Opened Files
  5. Save Cursor Position
  6. Dired Customization
  7. Open Recently Opened
  8. Open Last Closed File

appearance

  1. Show Line Numbers
  2. Show Cursor Position
  3. Visual Line Mode, Wrap Lines
  4. High Light Current Line
  5. Make Whitespaces Visible
  6. Set Default Window Size
  7. Font Setup
  8. Line Spacing
  9. Set Color Theme
  10. Highlight Paren
  11. Show lambda as λ
  12. Color CSS Hex Code

misc

  1. Default Browser
  2. Misc Init Tips

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

Emacs Lisp

Misc