One thing that really bugs me about Emacs is the way it clutters up my directories with backup files (filenames ending in ~) and autosave files (filenames starting with #). Fortunately there's an easy way to move them elsewhere. Unfortunately the technique isn't consistent across Emacs versions. In GNU Emacs 21, you can set backup-directory-alist and auto-save-file-name-transforms. In XEmacs 21, you can set bkup-backup-directory-info and auto-save-directory. Here's what I do in GNU Emacs:

(defvar user-temporary-file-directory
  (concat temporary-file-directory user-login-name "/"))
(make-directory user-temporary-file-directory t)
(setq backup-by-copying t)
(setq backup-directory-alist
      `(("." . ,user-temporary-file-directory)
        (,tramp-file-name-regexp nil)))
(setq auto-save-list-file-prefix
      (concat user-temporary-file-directory ".auto-saves-"))
(setq auto-save-file-name-transforms
      `((".*" ,user-temporary-file-directory t)))

Here's what I do in XEmacs:

(require 'auto-save) 
(require 'backup-dir) 

(defvar user-temporary-file-directory
  (concat (temp-directory) "/" (user-login-name)))
(make-directory user-temporary-file-directory t)
(setq backup-by-copying t)
(setq auto-save-directory user-temporary-file-directory)
(setq auto-save-list-file-prefix 
         (concat user-temporary-file-directory ".auto-saves-"))
(setq bkup-backup-directory-info
      `((t ,user-temporary-file-directory full-path)))

I'm much happier with Emacs temporary files being kept out of my way.

Labels:

9 comments:

Ian J wrote at April 19, 2007 11:36 PM

Just what I wanted - many thanks

Anonymous wrote at July 18, 2008 10:13 AM

Thanks for your help, that worked in version gnu 22.2.1.

James wrote at August 18, 2008 6:14 AM

I've wanted to fix that forever, but I'm no emacs expert. Thanks for posting it!

Anonymous wrote at September 24, 2008 6:41 AM

Thanks a lot!

Before I lost my .emacs, I had a version which adds the current date/time to the filename.
Do u have any idea how I can do this?

Max

Amit wrote at September 24, 2008 11:22 PM

Max: sorry, I don't know how to add the date/time. I just let the OS record the date/time and use that.

R. Mutt wrote at March 04, 2009 12:11 PM

Those backup files have saved my butt on more than one occasion and I don't find "rm *~" that much of a chore. Thanks fore exposing the settings, though!

Martyros wrote at March 19, 2009 3:40 AM

Be advised that for Debian-based systems (including ubuntu), the default is to clean out /tmp on every boot; so storing your autosave backup files there might not be a good idea. (Autosave especially, as you normally need them exactly when you've rebooted unexpectedly!) So users of those systems may want to set user-temporary-file-directory to something more permanent, thus:

(defvar user-temporary-file-directory
"~/.emacs-backup")

Vikraman wrote at June 15, 2009 6:35 AM

Thanks!

Sébastien wrote at September 28, 2009 10:51 AM

Thanks! I was using the function "make-backup-file-name" for my backup, but for some unknow reasons this function doesn't work anymore on my new computer.

Post a Comment