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:

14 comments:

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

Just what I wanted - many thanks

Anonymous wrote at Friday, July 18, 2008 at 10:13:00 AM PDT

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

James wrote at Monday, August 18, 2008 at 6:14:00 AM PDT

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

Anonymous wrote at Wednesday, September 24, 2008 at 6:41:00 AM PDT

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 Wednesday, September 24, 2008 at 11:22:00 PM PDT

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 Wednesday, March 4, 2009 at 12:11:00 PM PST

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 Thursday, March 19, 2009 at 3:40:00 AM PDT

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 Monday, June 15, 2009 at 6:35:00 AM PDT

Thanks!

S├ębastien wrote at Monday, September 28, 2009 at 10:51:00 AM PDT

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.

Anonymous wrote at Friday, October 1, 2010 at 12:10:00 AM PDT

I am running emacs 23.1 on Windows, accessing files on a network (Linux) drive. This method seems to not work for files on the network drive (L:\), but works fine for files on the local harddrive (C:\) ??

Amit wrote at Friday, October 1, 2010 at 1:49:00 PM PDT

Hi Anonymous — yes, there are all sorts of weird things that happen with network drives, unfortunately. It's not just Emacs; it's that network drive protocols don't always support the full set of file operations. I'm afraid I don't have any suggestions for you :(

Sam wrote at Thursday, March 17, 2011 at 1:25:00 AM PDT

Thank you, works great

jolisper wrote at Wednesday, August 22, 2012 at 11:12:00 PM PDT

Hey, thanks! That is what I'm looking for.

jolisper wrote at Wednesday, August 22, 2012 at 11:14:00 PM PDT

Thanks! That is what I'm looking for