1

I am aware of How to place all auto-save files in a directory, but I cannot figure it out from Emacs manual how to save all auto-save versions and all backup copies of a given file in a subdirectory sitting in same parent directory where the file resides?

For example, if I am editing file etiology-macro.lisp, I would need to keep all auto-save and backup versions of that file in subdirectory auto-save-and-backup-subdirectory

path/to/directory/
          etiology-macro.lisp
          auto-save-and-backup-subdirectory/
              etiology-macro.lisp.20170827-132731
              etiology-macro.lisp.20170827-133007
              etc.

How to set auto-save and backup path as subdirectory sibling of a given open file?

wasamasa
  • 21,803
  • 1
  • 65
  • 97
gsl
  • 1,742
  • 17
  • 34

2 Answers2

1

This will configure things to save both backup and auto-save files to an emacs-backups directory relative to the file you are visiting.

(let ((dir "emacs-backups"))
  (setq auto-save-file-name-transforms `(("\\([^/]*/\\)*\\([^/]*\\)\\'" ,(concat dir "/\\2")))
        backup-directory-alist `((".*" . ,dir))))

Note that for backup files you can just specify a directory (absolute or relative). For auto-save files you specify a transformation using a regex against the full path of the buffer file. In the above example I'm capturing the file name (without the parent dirs) and then referencing that in the replacement regex (\2) to append it to the relative directory.

glucas
  • 20,175
  • 1
  • 51
  • 83
  • Sorry for noticing this only now, I though sx was sending email upon new answers. Would it be possible to add the date timestamp, like in original question, ie, `etiology-macro.lisp.20170827-133007`? Thank you very much. – gsl Dec 04 '17 at 15:32
  • 1
    Autosave files are temporary (they get cleaned up when you actually save the file) so I'm not sure a timestamped naming strategy makes sense for those? But it does seem potentially useful for backups: in that case you would want to configure how many backups are kept (see variables `kept-old-versions` and `kept-new-versions`, as well as the `delete-old-versions` option.) For backup file names I think you would need to write a function to implement your naming strategy, and set `make-backup-file-name-function` to use it. – glucas Dec 04 '17 at 16:21
  • Thank you for the pointer and information on autosave files. I will try to implement `make-backup-file-name-function` and report back. – gsl Dec 04 '17 at 16:51
0

backup-each-save.el script at EmacsWiki is the closest answer I could find for now.

From the inline comment:

;; This package copies every file you save
;; in Emacs to a backup directory tree (which mirrors the tree
;; structure of the filesystem), with a timestamp suffix to make
;; multiple saves of the same file unique. 

I am still looking for a way to set auto-save and backup path as subdirectory sibling of a given open file.

gsl
  • 1,742
  • 17
  • 34