Emacs: Turn Off Emacs Auto Backup~

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)

configure backups into one folder

Put this in your Emacs Init File:

;; backup in one place. flat, no tree structure
(setq backup-directory-alist '(("" . "~/.emacs.d/backup")))

This will create backup files flat in the given dir, and the backup file names will have EXCLAMATION MARK ! characters in place of the directory separator.

For example:

If you use long file names or many nested dirs, this scheme will reach file name length limit quickly. (Mac and Windows allow 255 chars for file name.)

set backups in one dir with tree structure

put this in your Emacs Init File:

;; make backup to a designated dir, mirroring the full path

(defun xah-backup-nested-dir-file-path (Fpath)
  "Return a new file path and create dirs.
If the new path's directories does not exist, create them.
version 2022-06-09"
  (let* ($backupRoot $backupFilePath)
    (setq $backupRoot "~/.emacs.d/backup/")
    ;; remove Windows driver letter in path, e.g. C:
    (setq $backupFilePath
          (format "%s%s~" $backupRoot (replace-regexp-in-string "^[A-Za-z]:/" "" Fpath)))
     (file-name-directory $backupFilePath)
     (file-name-directory $backupFilePath))

(setq make-backup-file-name-function 'xah-backup-nested-dir-file-path)

The above will mirror tree structure to the backup dir.

For example:

(info "(elisp) Backup Files") (info "(emacs) Backup")

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

Emacs: auto save, backup

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Init

Init Basics



Text Editing



Restore Things





Advanced Init Tips