1

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:

~/.saves/home/bjourne/wtf.txt/~1~
~/.saves/home/bjourne/wtf.txt/~2~
~/.saves/home/bjourne/wtf.txt/~3~
~/.saves/home/bjourne/whatever/program.c/~1~
~/.saves/home/bjourne/whatever/program.c/~2~

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

2 Answers2

1

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.

icarus
  • 1,904
  • 1
  • 10
  • 15
0

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

glucas
  • 20,175
  • 1
  • 51
  • 83
  • 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." – Björn Lindqvist Oct 26 '16 at 19:23
  • It worked for me on emacs 25.1 with the code shown. Though as noted it's an incomplete solution. – glucas Oct 26 '16 at 22:41