Emacs: Turn Off Auto Backup; Set Backups into a Directory; How to Delete Backup Files

By Xah Lee. Date: . Last updated: .

How to make emacs stop creating “backup~” files or “#autosave#” files?

Put the following in your emacs init file.

(setq make-backup-files nil) ; stop creating backup~ files
(setq auto-save-default nil) ; stop creating #autosave# files

How to set emacs so that all backups are placed in one place?

Put the following in your emacs init:

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

This will create backup files flat in the given dir, and the backup file names will have “!” 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.)

How to set emacs so backups in one place with tree structure?

Put the following in your emacs init file:

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

(defun my-backup-file-name (fpath)
  "Return a new file path of a given file path.
If the new path's directories does not exist, create them."
  (let* (
        (backupRootDir "~/.emacs.d/emacs-backup/")
        (filePath (replace-regexp-in-string "[A-Za-z]:" "" fpath )) ; remove Windows driver letter in path, for example, “C:”
        (backupFilePath (replace-regexp-in-string "//" "/" (concat backupRootDir filePath "~") ))
    (make-directory (file-name-directory backupFilePath) (file-name-directory backupFilePath))

(setq make-backup-file-name-function 'my-backup-file-name)

The above will mirror tree structure to the backup dir.

For example:

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

How to Delete Emacs Backup Files

To delete all backup files in current dir:

  1. Alt+x dired.
  2. dired-flag-backup-files~】 → mark all backup files for deletion.
  3. dired-do-flagged-deletex】 → (execute) delete files flagged for deletion.

[see Emacs: File Manager, dired]

Or, you can use the linux shell command:

# delete all file whose name end in ~. All subdir too.
find . -name "*~" -delete

You can setup a abbrev to quickly type the shell line. See: Emacs: Abbrev Mode Tutorial.

Even more efficient is to set a command that lets you pick a shell command. See: Emacs: Interactive Abbrev.

Backup on Demand

Emacs: Backup Current File

Emacs Customization

  1. How to Set Emacs's User Interface
  2. Emacs: How to Install Packages Using ELPA, MELPA
  3. How to Install Emacs Package Manually
  4. Emacs: How to Define Keys
  5. Emacs: M-x customize Tutorial
  6. Emacs: Set File to Open in a Major Mode
  7. Emacs: Organize Init File
  8. Emacs: Byte Compile Elisp Files
  9. Emacs: What's Hook?
  10. Emacs: Set Environment Variables within Emacs
  11. Elisp: Determine OS, Emacs Version, Machine Host Name
  12. Elisp: Check If a {function, variable, feature} is Defined/Loaded
  13. Emacs: Set Default Window (frame) Size
  14. Emacs: Font Configuration
  15. Emacs: Set Color Theme
  16. Emacs: Save Cursor Position
  17. Emacs: Turn Off Auto Backup; Set Backups into a Directory; How to Delete Backup Files
  18. Emacs: Stop Cursor Going into Minibuffer Prompt
Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.