5.39 Making Emacs write all auxiliary files somewhere else

By default, Emacs may create many new files in the directory where you’re editing a file. If you’re editing the file /home/user/foo.txt, Emacs will create the lock file /home/user/.#foo.txt, the auto-save file /home/user/#foo.txt#, and when you save the file, Emacs will create the backup file /home/user/foo.txt~. (The first two files are deleted when you save the file.)

This may be inconvenient in some setups, so Emacs has mechanisms for changing the locations of all these files.

auto-save-file-name-transforms

(see Auto-Saving in GNU Emacs Lisp Reference Manual).

lock-file-name-transforms

(see File Locks in GNU Emacs Lisp Reference Manual).

backup-directory-alist

(see Making Backups in GNU Emacs Lisp Reference Manual).

For instance, to write all these things to ~/.emacs.d/aux/:

(setq lock-file-name-transforms
      '(("\\`/.*/\\([^/]+\\)\\'" "~/.emacs.d/aux/\\1" t)))
(setq auto-save-file-name-transforms
      '(("\\`/.*/\\([^/]+\\)\\'" "~/.emacs.d/aux/\\1" t)))
(setq backup-directory-alist
      '((".*" . "~/.emacs.d/aux/")))