Emacs: Auto Backup~ (tilde postfix)

By Xah Lee. Date: . Last updated: .

disable emacs automatic backup~ file

Emacs by default automatically creates backup files in the same dir, and with filename ending in TILDE ~.

To turn it off, put this in your Emacs Init File:

(setq make-backup-files nil)
;; default is t

Multi Backup (Whenever File is Saved)?

Emacs only make one backup, when file is changed for first time, not every save.

There is no simple config to make emacs do a backup whenever file is saved. You have to add a Hook to save-buffer. [see Emacs: Backup Current File 📜]

backup into one folder

Put this in your Emacs Init File:

;; backup into one flat dir
(setq backup-directory-alist '(("." . "~/.emacs.d/backup/")))
;; example
;; file
;; /A/B/C/ff.txt
;; backup root dir
;; ~/.emacs.d/backup
;; backup at
;; ~/.emacs.d/backup/A!B!C!ff.txt~

stop emacs backup changing the file's creation date of the original file

Put this in your Emacs Init File:

(setq backup-by-copying t)

Explanation: when emacs does a backup, by default it renames the original file into the backup file name, then create a new file and insert new file content into it. This effectively destroys the creation date of your file. (If a file is created in 2001, and you modified it today, the file's creation date will become today.

Note: unixes (including Linux and BSD) do not record file creation date, so this doesn't matter. (ctime is not creation date.) Microsoft Windows and Mac OS X do record file creation date.).

All Configurations for Backup

Here are all configurations for backup.

(setq make-backup-files t)

;; make backup name incremental by number, ending in ~1~ ~2~ etc
;; note: only one backup is made when file is changed for first time, not every save
(setq version-control nil)

;; make sure hard link and creation date, owner, etc is preserved
(setq backup-by-copying t)

;; make backup even in git controlled dir
(setq vc-make-backup-files t)

;; silently delete old backup
(setq delete-old-versions t)

;; (setq kept-old-versions 2) ; default 2
;; (setq kept-new-versions 2) ; default 2

;; function that decide a file should be backed up
;; (setq backup-enable-predicate 'normal-backup-enable-predicate )

;; a alist (regex . dir) to decide where to place backup
;; (setq backup-directory-alist '(("." . "~/.emacs.d/backup")))

Reference

Emacs, auto save, backup