I'm putting all my backups in a directory:

(defvar saves-dir (expand-file-name "~/.saves/"))
(setq backup-directory-alist `((".*" . ,saves-dir)))

This creates annoying filenames with ! in them, such as !home!bjourne!.fonts.conf.~1~. How do I change that so it instead becomes _home_bjourne_.fonts.conf.~1~.?

Come to think of it, even better would be if emacs created a directory tree and kept each backup file in its own directory. Like this:


So if this naming scheme was possible, I'd prefer that.

2 Answers 2


You can customize themake-backup-filename-function variable to do this, using the existing make-backup-file-name and make-backup-file-name-1 as examples. If you don't need to support msdos etc then you can simplify them a bit.

My thought this is a lot of work for little gain. It would be different if ! meant something to your filesystem.

  • Please write example code because I did that and it has no effect on emacs. Commented Oct 26, 2016 at 7:20

+1 for the answer from @icarus. Here's an example from the Emacs Wiki that stores your backup files in dated directories (e.g. ~/.backups/emacs/16/10/26/foo.txt).

(defun my-make-backup-file-name (FILE)
  (let ((dirname (concat "~/.backups/emacs/"
                         (format-time-string "%y/%m/%d/"))))
    (if (not (file-exists-p dirname))
        (make-directory dirname t))
    (concat dirname (file-name-nondirectory FILE))))

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

Note that unlike the default backup name function, this doesn't handle the case where you have multiple files with the same name at different paths -- they would map to the same backup. You probably want to look at make-backup-file-name-1 (where path separators are replaced with !) to make the simple example above more robust.

  • I contend that this does not work. It creates a file called ~/.backups/emacs/16/10/26/.ido.last but nothing else there. Docs for make-backup-file-name-function says "If you change this, you may need to change ‘backup-file-name-p’ and ‘file-name-sans-versions’ too." Commented Oct 26, 2016 at 19:23
  • It worked for me on emacs 25.1 with the code shown. Though as noted it's an incomplete solution.
    – glucas
    Commented Oct 26, 2016 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.