<rdf:Description rdf:about="http://snarfed.org/space/gnu%20emacs%20backup%20files">
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Ryan Barrett &lt;snarfed at ryanb dot org&gt; </dc:creator>
  <dc:date> 2003-01-01T05:00:00Z </dc:date>
  <dc:language> en </dc:language>
  <dc:format> text/html </dc:format>
  <dc:rights> Copyright 2002-2009 Ryan Barrett </dc:rights>

  <content>
    <p><a href="http://gnu.org/software/emacs/"><img src="/space/gnu.jpg" alt="/space/gnu.jpg" title="" /></a></p>

<p>I use <a href="http://gnu.org/software/emacs/">GNU
Emacs</a>, and I love it. However, one annoyance is
that it scatters autosave files (<em>#foo#</em>) and backup files (<em>foo~</em>) all over the
filesystem.</p>

<p>So, I finally got fed up with this, and figured out how to keep them all in one
place. Here's the
<a href="http://gnu.org/software/emacs/emacs-lisp-intro/html_node/index.html">elisp</a>
(GNU Emacs only) that I put in <a href="/space/dotfiles/emacs">my
.emacs</a>:</p>

<div class='p-shadow'><pre><code>;; 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=&gt;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)))
</code></pre></div>

<p><br class='clearing' />...and, courtesy of <a href="http://www-cs-students.stanford.edu/~amitp/">Amit Patel</a>,
here's the elisp for XEmacs.</p>

<div class='p-shadow'><pre><code>;;; 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)
</code></pre></div>

<p><br class='clearing' /></p>

  </content>

  <rdf:Seq>

<rdf:li>
<rdf:Description rdf:about="#1149864373.0">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files-1 </dc:title>
  <dc:creator> Guest </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    
Thanks a ton! that sure is helpful!<p class="paragraph"/> <i class="italic">- Sandeep</i>

  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1158565457.97">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> traneHead </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thank you!!
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1161115682.91">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Christian Betz </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    You rule!
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1172456715.89">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Jacob </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Finally, a script that makes this simple.<br />
<br />
Thank you.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1172728825.27">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Justin </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    That's awesome... saves me a lot of hassle
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1175628365.06">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Redtoade </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thanks a ton!<br />
Nuisance gone.<br />
<br />
What's the reason behind different scripts for XEmacs and GNU Emacs?&nbsp; I knew that the two don't play nice, but I'm surprised that the .el files are tailored for one or the other.<br />
<br />
Can't wait to see how they work on EmacsW32!
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1175636694.52">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Ryan </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    good point! they're different because i only wrote the gnu emacs elisp. i provided amit's xemacs elisp for completeness and convenience only.<br />
<br />
having said that, the gnu emacs code evaluates happily in xemacs, so it just might work. amit's code, on the other hand, uses <code>auto-save.el</code>, which comes with xemacs but not gnu emacs. it might be a better solution, but it's xemacs-specific.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1182889810.86">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Redtoade </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    It's amazing how such a simple piece of code eliminates such a pain in the butt problem.&nbsp; Thanks again.<br />
<br />
I have been thinking though.&nbsp; (No good deed and what not...)<br />
<br />
I use individual .emacs files per user.&nbsp; Each user adds your snippet.&nbsp; Therefore, without any alterations, each user gets a (user-login-name) added to both the /tmp/emacs_backups and /tmp/emacs_autosaves.<br />
<br />
For me, these files are made with the default mask in bash, I guess.&nbsp; So they get permissions of 755.&nbsp; No matter what the original file was.&nbsp; Since /tmp is a public directory, now anyone can read the backups.<br />
This might be a security risk.<br />
<br />
I'm no bash expert.&nbsp; So I'm no help.&nbsp; Sorry.<br />
<br />
Thanks again.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1189525261.38">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Arthur </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    I had a problem where emacs would try and auto-save files to /tmp/emacs_autosaves/arthur//home/arthur/<b>scratch</b> 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.<br />
<br />
(defun make-auto-save-file-name ()<br />
&nbsp; (concat autosave-dir<br />
	&nbsp; (if buffer-file-name<br />
	&nbsp; (concat "#" (file-name-nondirectory buffer-file-name) "#")<br />
&nbsp; (concat "#%" (buffer-name) "#"))))<br />
<br />
<br />
Thanks for the script.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1191575312.43">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Amir </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thanks a lot for the script.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1192677397.6">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Mike </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    It seems that putting them in a central directory puts bangs (!)'s in the file name.&nbsp; How can you restore a backup in this case, given that ! is a control character in *nix?
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1192727536.55">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> ryan </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    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 - <code>cat "/tmp/bang!file"</code> - or escape the ! - <code>cat /tmp/bang\!file</code>.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1197707660.09">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Noam </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    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.<br />
<br />
<code>(setq auto-save-file-name-transforms `(("\\(?:[^/]*/\\)*\\(.*\\)" ,(concat autosave-dir "\\1") t)))</code><br />
<br />
Acheives the same goal as the defuns, without breaking autosaves for gnus or tramp.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1203592352.26">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> sam17 </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thanks for this little tool.<br />
<br />
It helped me at work where non emacs users colleagues complains about all these little files...
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1230696607.28">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Tushar </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thanks dude ! This is really helpful ! All those crap files are gone ! :-)
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1236781819.88">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> 164747 </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thank you!<br />
This has been haunting me for years. If one wants to save other users on the system from this plague, you can copy the above code to /etc/emacs/site-start.el instead of into to ~/.emacs file.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1242994215.25">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> G. Georgiev </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thanks!<br />
For years I did just live with that nuisance - in reality small one considering all the functionality that Emacs offers! - but it is better now. - George.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1243452618.58">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> David McCracken </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    setq auto-save-file-name-transforms only works for the auto-save (#) files. The default make-backup-file-name function, defined in files.el, invokes make-backup-file-name-1, which embeds the !path! file name synthesis so there is no way to change these long names for backup (~) files without completely replacing make-backup-file-name function. To shrink auto-save names, the last element of auto-save-file-name-transforms should be nil instead of t. Also, note that the RE is for DOS. For LINUX I use:<br />
(setq auto-save-file-name-transforms `(("\\([^/]*/\\)*\\(.*\\)", "~/bak/\\2" nil))) making auto-saves userhome\bak\#filename#.
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1243820510.64">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Tubo </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thanks a lot!<br />
Wonderful work!
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1245072828.78">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Vikraman </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    Thanks!
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1266399537.41">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> Memnon </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    I don't get it.<br />
So many people seem to have 'just lived' with it.<br />
Everyone: The /info/ is right there, it is even<br />
in the FAQ. 'C-h i' at your service:<br />
,----[ (info "(efaq)Disabling backups") ]<br />
|&nbsp; Beginning with Emacs 21.1, you can control where Emacs puts backup<br />
| files by customizing the variable `backup-directory-alist'.&nbsp; This<br />
| variable's value specifies that files whose names match specific patters<br />
| should have their backups put in certain directories.&nbsp; A typical use is<br />
| to add the element `("." . DIR)' to force Emacs to put <b>all</b> backup<br />
| files in the directory `dir'.<br />
`----
  </content>
</rdf:Description>
</rdf:li>

<rdf:li>
<rdf:Description rdf:about="#1266429537.31">
  <dc:source> http://snarfed.org/ </dc:source>
  <dc:title> gnu emacs backup files </dc:title>
  <dc:creator> ryan </dc:creator>
  <dc:date> cmt_pubDate </dc:date>
  <dc:format> text/html </dc:format>

  <content>
    uh, did you read the post? that's what it does. the big extra chunk of elisp at the beginning is for <i>autosave</i> files, not backups.
  </content>
</rdf:Description>
</rdf:li>

  </rdf:Seq>
</rdf:Description>
</rdf:RDF>
