I defined these lines in my .emacs initialization file to change the location for backup and auto-save files.

(defvar BACKDIR (expand-file-name "~/emacs_backup/"))
(setq backup-directory-alist `((".*" . ,BACKDIR)))
(setq auto-save-file-name-transforms `((".*" ,BACKDIR t)))

However, i got an error all the time it is gonna perform an auto-save as it follows:

Auto-saving define_transistors.tcl: Opening output file: no such file or directory, /home/SchoolServer/UserID/emacs_backup/home/SchoolServer/UserID/Projects/char_tst/charscript/#!home!SchoolServer!UserID!Projects!char_tst!charscript!define_transistors.tcl#

Any idea how to fix it?

1 Answer 1


You should probably have a more explicit regex in auto-save-file-name-transforms. The regex matches against the buffer file name. So what's happening in your case is

  1. Your buffer filename is: "/home/SchoolServer/UserID/Projects/char_tst/charscript/define_transistors.tcl"
  2. Your regex is: ".*" ie. "zero or more characters"
  3. That regex will match the start of the string
  4. The transform that you have described replaces the start of the string with the value of BACKDIR. This gives you the string "/home/SchoolServer/UserID/emacs_backup/home/SchoolServer/UserID/Projects/char_tst/charscript/#/home/SchoolServer/UserID/Projects/char_tst/charscript/define_transistors.tcl#"
  5. The transform then tries to "uniquify" the string by changing the filename component of the string, giving you "/home/SchoolServer/UserID/emacs_backup/home/SchoolServer/UserID/Projects/char_tst/charscript/#!home!SchoolServer!UserID!Projects!char_tst!charscript!define_transistors.tcl#"
  6. auto-save then tries to use this string as a path, and obviously it doesn't work because while "/home/SchoolServer/UserID/emacs_backup" exists (your BACKDIR) it doesn't have the subdirectories "home/SchoolServer/UserID/Projects/char_tst/charscript/" so the file "#!home!SchoolServer!UserID!Projects!char_tst!charscript!define_transistors.tcl#" can't be written.

In order to fix this situation, I would look at some default values. Emacs uses temporary-file-directory as the auto-save location. This usually is pulled from your environment (ie TEMPDIR), and has some hard coded fallback. Emacs 26.x on my system has the following for auto-save-file-name-transforms:

(("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'" "/var/folders/vk/8b7cwwrx2b93zglg16rs57pr0000gn/T/\\2" t))

You'll notice that the regex is capturing the filename component of the buffer filename. That capture is then being used in the replacement text (\\2). Aquamacs is does it similarly.

(("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'" "~/Library/Caches/Aquamacs Emacs/AutoSave/\\2" t)
 ("\\`\\([^/]*/\\)*\\([^/]*\\)\\'" "~/Library/Caches/Aquamacs Emacs/AutoSave/\\2" t))

Again, the filename components are being captured in the regex and reused in the replacement. So, in your case, you might want to try:

;; override auto-save-file-name-transforms
(setq auto-save-file-name-transforms 
  `(("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'" ,(concat BACKDIR "\\2") t)))
;; or, you could prepend instead!
(setq auto-save-file-name-transforms
  (cons `(,(car (car auto-save-file-name-transforms)) 
          ,(concat BACKDIR "\\2") t) auto-save-file-name-transforms))

You'll know the best way to do it for you.

  • 1
    Tnx for your super complete answer to my question!
    – manpmanp
    Commented Feb 27, 2019 at 10:23

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.