snarfed.org

draw group stream of consciousness

gnu emacs backup files

Wed, 01 Jan 2003 [comments (14)] [history] [rdf] [raw]

/space/gnu.jpg

I use GNU Emacs, and I love it. However, one annoyance is that it scatters autosave files (#foo#) and backup files (foo~) all over the filesystem.

So, I finally got fed up with this, and figured out how to keep them all in one place. Here's the elisp (GNU Emacs only) that I put in my .emacs:

;; Put autosave files (ie #foo#) in one place, *not*
;; scattered all over the file system!
(defvar autosave-dir
 (concat "/tmp/emacs_autosaves/" (user-login-name) "/"))

(make-directory autosave-dir t)

(defun auto-save-file-name-p (filename)
  (string-match "^#.*#$" (file-name-nondirectory filename)))

(defun make-auto-save-file-name ()
  (concat autosave-dir
   (if buffer-file-name
      (concat "#" (file-name-nondirectory buffer-file-name) "#")
    (expand-file-name
     (concat "#%" (buffer-name) "#")))))

;; Put backup files (ie foo~) in one place too. (The backup-directory-alist
;; list contains regexp=>directory mappings; filenames matching a regexp are
;; backed up in the corresponding directory. Emacs will mkdir it if necessary.)
(defvar backup-dir (concat "/tmp/emacs_backups/" (user-login-name) "/"))
(setq backup-directory-alist (list (cons "." backup-dir)))


...and, courtesy of Amit Patel, here's the elisp for XEmacs.

;;; Auto-save
;;; Load the auto-save.el package, which lets you put all of your autosave
;;; files in one place, instead of scattering them around the file system.
;;; M-x recover-all-files or M-x recover-file to get them back
(defvar temp-directory (concat "/tmp/" (user-login-name)))
(make-directory temp-directory t)

; One of the main issues for me is that my home directory is
; NFS mounted.  By setting all the autosave directories in /tmp,
; things run much quicker
(setq auto-save-directory (concat temp-directory "/autosave")
      auto-save-hash-directory (concat temp-directory "/autosave-hash")
      auto-save-directory-fallback "/tmp/"
      auto-save-list-file-prefix (concat temp-directory "/autosave-")
      auto-save-hash-p nil
      auto-save-timeout 100
      auto-save-interval 300)
(make-directory auto-save-directory t)
(require 'auto-save)

;;; Put backups in another directory.  With the directory-info
;;; variable, you can control which files get backed up where.
(require 'backup-dir)
(setq bkup-backup-directory-info
      `(
        (t ,(concat temp-directory "/backups") ok-create full-path)
        ))
(setq make-backup-files t)
(setq backup-by-copying t)
(setq backup-by-copying-when-mismatch t)
(setq backup-by-copying-when-linked t)
(setq version-control t)
(setq-default delete-old-versions t)


comment bubble OpenID Guest, Fri 09 Jun 2006

Thanks a ton! that sure is helpful!

- Sandeep

comment bubble OpenID traneHead, Mon 18 Sep 2006

Thank you!!

comment bubble OpenID Christian Betz, Tue 17 Oct 2006

You rule!

comment bubble OpenID Jacob, Mon 26 Feb 2007

Finally, a script that makes this simple.

Thank you.

comment bubble OpenID Justin, Thu 01 Mar 2007

That's awesome... saves me a lot of hassle

comment bubble OpenID Redtoade, Tue 03 Apr 2007

Thanks a ton!
Nuisance gone.

What's the reason behind different scripts for XEmacs and GNU Emacs?  I knew that the two don't play nice, but I'm surprised that the .el files are tailored for one or the other.

Can't wait to see how they work on EmacsW32!

comment bubble OpenID Ryan, Tue 03 Apr 2007

good point! they're different because i only wrote the gnu emacs elisp. i provided amit's xemacs elisp for completeness and convenience only.

having said that, the gnu emacs code evaluates happily in xemacs, so it just might work. amit's code, on the other hand, uses auto-save.el, which comes with xemacs but not gnu emacs. it might be a better solution, but it's xemacs-specific.

comment bubble OpenID Redtoade, Tue 26 Jun 2007

It's amazing how such a simple piece of code eliminates such a pain in the butt problem.  Thanks again.

I have been thinking though.  (No good deed and what not...)

I use individual .emacs files per user.  Each user adds your snippet.  Therefore, without any alterations, each user gets a (user-login-name) added to both the /tmp/emacs_backups and /tmp/emacs_autosaves.

For me, these files are made with the default mask in bash, I guess.  So they get permissions of 755.  No matter what the original file was.  Since /tmp is a public directory, now anyone can read the backups.
This might be a security risk.

I'm no bash expert.  So I'm no help.  Sorry.

Thanks again.

comment bubble OpenID Arthur, Tue 11 Sep 2007

I had a problem where emacs would try and auto-save files to /tmp/emacs_autosaves/arthur//home/arthur/scratch or similar directories. Emacs could not open the file because the directories did not exist. I fixed it by changing make-auto-save-file-name to not add paths to non-file buffers. I'm not where why that was there in the first place.

(defun make-auto-save-file-name ()
  (concat autosave-dir
  (if buffer-file-name
  (concat "#" (file-name-nondirectory buffer-file-name) "#")
  (concat "#%" (buffer-name) "#"))))


Thanks for the script.

comment bubble OpenID Amir, Fri 05 Oct 2007

Thanks a lot for the script.

comment bubble OpenID Mike, Thu 18 Oct 2007

It seems that putting them in a central directory puts bangs (!)'s in the file name.  How can you restore a backup in this case, given that ! is a control character in *nix?

comment bubble OpenID ryan, Thu 18 Oct 2007

hmm, i don't understand what you mean by "control character in *nix." do you mean it's a control character in your shell? just quote the filename - cat "/tmp/bang!file" - or escape the ! - cat /tmp/bang\!file.

comment bubble OpenID Noam, Sat 15 Dec 2007

I used this code for a while, it works but causes error message in mail buffers. After some investigation I think that this code does the Wrong Thing. The default implementation of make-auto-save-file-name is much more sophisticated, and it uses the variable auto-save-file-name-transforms which allows the user to change the outcome without redefining functions.

(setq auto-save-file-name-transforms `(("\\(?:[^/]*/\\)*\\(.*\\)" ,(concat autosave-dir "\\1") t)))

Acheives the same goal as the defuns, without breaking autosaves for gnus or tramp.

comment bubble OpenID sam17, Thu 21 Feb 2008

Thanks for this little tool.

It helped me at work where non emacs users colleagues complains about all these little files...

Post a comment...



Simple HTML and wiki markup are allowed.