AutoSave

By default, Emacs automatically saves your changes to a file intermittently. If anything should happen, you can recover a file with ‘M-x recover-file’. Auto-saving can be turned on globally or on a per-buffer basis with ‘M-x auto-save-mode’. Note that auto-saving and backing up are different concepts and serve different purposes.

See also the Emacs auto-save manual entry.

Placing all files in one directory

By default, auto-save files are stored in the current directory with a file name on the form #file#. If you don’t want to clutter up your file tree with Emacs’ backup files, you can save them to the system’s “temp” directory:

    (setq backup-directory-alist
          `((".*" . ,temporary-file-directory)))
    (setq auto-save-file-name-transforms
          `((".*" ,temporary-file-directory t)))

This will all place all auto-saves and backups in the directory pointed to by temporary-file-directory (e.g., C:/Temp/ on Windows).

Or even more dramatic:

    ;; Save all tempfiles in $TMPDIR/emacs$UID/                                                        
    (defconst emacs-tmp-dir (format "%s/%s%s/" temporary-file-directory "emacs" (user-uid)))
    (setq backup-directory-alist
        `((".*" . ,emacs-tmp-dir)))
    (setq auto-save-file-name-transforms
        `((".*" ,emacs-tmp-dir t)))
    (setq auto-save-list-file-prefix
        emacs-tmp-dir)

EDIT: I think "%s/%s%s/" should be replace by "%s%s%s/" as ‘temporary-file-directory’ comes with‘/’ already (tested 24.3.93 windows and linux).

Delete auto-save files

          find -name "#*#" | xargs rm
If the file names contain whitespace, it is necessary to use one of:
          find -name "#*#" -print0 | xargs -0 rm
          find -name "#*#" -exec rm {} \;

Interlock symlinks and auto-save files

Be aware that symbolic links of the form “.#*” are not auto-save files but interlocks to prevent the simultaneous editing of the same file. See the Emacs manual entry for details.

Discussion

How do I turn on auto-saving globally or on a per-buffer basis?

See Auto Save Control, it should be on by default, but you can turn it off in a buffer with ‘M-x auto-save-mode’. – AaronHawley

Problems with Tramp

Hello, I’m trying to use emacs to edit a file across a locally-mounted network file system. There is a long latency across the connection, so I need to minimize file accesses over the network. However, if I open file “abcd”, emacs creates files like .#abcd, .#abcd.2, etc, in the directory housing the file. This makes things take forever, because each file creation/access in that directory goes across the network. How do I tell emacs to either not create those files, or at the least to create them in /tmp rather than in the same directory as the file? – BayleShanks?

See auto-save-file-name-transforms. The default value matches all TRAMP URLs. With a little bit of tweaking, you can add another sexp for your locally-mounted network shares. I use the following incantation to save all my temporary files to another partition, as I occasionally have problems with my data partition.

   (setq auto-save-file-name-transforms
         '(("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'" "/tmp/\\2" t)
          ("\\`/?\\([^/]*/\\)*\\([^/]*\\)\\'" "/usr/local/sacha-backup/\\2" t)))

SachaChua

Disabling Auto Save

Every time I edit a file and then discard the changes emacs saves a ~/#filename# file. The next time I run emacs it warns me about it and advises me to recover the file. How can I disable this “feature” ?

That is really two features, “auto save”(1) and “find-file warning messages”(2). Auto-save is easy to disable, just add (setq auto-save-default nil) to your .emacs file. The warning messages are a bit trickier to suppress. There’s an optional parameter in find-file-noselect …

Running save-buffer automatically

All these new-fangled IDEs tout that they automatically save your files for you. I think this would be cool to try out (my c-x c-s muscles get tired sometimes). You can set auto-save-visited-file-name to a non-nil value and emacs auto-save will save your work in the file you are working on instead of a separate file, but then the buffer is considered out of date and you can’t continue editing without an annoying warning message. Is there a way around this? – BryanMurdock

What about ‘M-x run-with-idle-timer RET SECONDS RET save-buffer RET’? If that’s what you want, then find a way to roll some EmacsLisp to make it work for all your buffers associated with files. Then you can put that in your DotEmacs and smoke it. – AaronHawley

My emacs lisp skills are feeble, but how does this look?

   (defun save-buffer-if-visiting-file (&optional args)
      "Save the current buffer only if it is visiting a file"
      (interactive)
      (if (and (buffer-file-name) (buffer-modified-p))
          (save-buffer args)))
   (add-hook 'auto-save-hook 'save-buffer-if-visiting-file)

It seems to work for me with both regular files and file you visited with tramp. – BryanMurdock

I like this. Also tried it with these settings, so that everything is just always saved.

    (setq auto-save-interval 1
          auto-save-timeout 1)

This works. However, I find it very annoying that the saving message is displayed every time I do anything. Thinking about it, continuous save should be silent. Also, it would be nice if there was a way to save a checkpoint that you could come back to. I often go off on an editing tangent, and then either undo until the “dirty” flag is cleared or just “M-x revert-buffer” to go back to before the madness - neither approach works when everything is always saved. I already use versioned backups in a separate directory; maybe the “checkpoint” could just be one of those. Bind “C-x C-s” to create a versioned backup (since manual saving is not needed) and provide a way to step back and forth through versions.

So: 1. Silent continuous save; 2. A function to create a versioned backup on demand - bound to “C-x C-s”; 3. A function or functions to step forward and backward through versioned backups. Anyone have thoughts on how to do these things, or if they are even the correct goal?

Automatically save your changes after an customizable interval. Can be selectively applied some modes.

I really like this idea, because it’s dumb to make me hit C-x C-s all the time because of technical details of the computer’s architecture, and almost everything I edit is under version control anyway. I like using auto-save-hook, and that seems to work well, but I want to save all the buffers, because if I switch away from a buffer before it autosaves, I want it to get saved when things next get saved after that. So I combined parts of the two above solutions (this is the first elisp code I’ve written, could have glaring problems):

	(defun full-auto-save ()
	  (interactive)
	  (save-excursion
		(dolist (buf (buffer-list))
		  (set-buffer buf)
		  (if (and (buffer-file-name) (buffer-modified-p))
			  (basic-save-buffer)))))
	(add-hook 'auto-save-hook 'full-auto-save)

Save on Loss of Input Focus

Focus Hooks

New hooks for gain/loss of focus were added in Gnu Emacs 24.4: focus-in-hook and focus-out-hook.

If you are running a version of Emacs with these hooks, you can add

  (defun save-all ()
    (interactive)
    (save-some-buffers t))
  (add-hook 'focus-out-hook 'save-all)

to your .emacs file and it should save all files on loss of focus.

Two things to note:

These hooks fire when any Emacs frame gains or loses focus. This means that if you Alt-Tab or Alt-` between Emacs frames, the focus-out-hook and focus-in-hook will run. (MacOS? and Windows have a concept of application-level focus, but X apparently does not. These hooks apply at the frame level so they can work on all three platforms.)

These hooks are intended for use by the end-user, not for use in packages. If you set these hooks for other people, they’re likely to be annoyed. (Particularly if they are debugging Emacs itself or working in a “focus follows mouse” environment where merely mousing over a window switches focus to it.)

User-Defined Signals

Another approach is tell emacs to do full-auto-save when it receives the USR1 signal.

  (define-key special-event-map [sigusr1]
        (lambda ()
          (interactive)
          (full-auto-save)))

Create a shell script to send the signal to emacs.

  #!/usr/bin/env sh
  for a in $(ps -au "<name>" | gawk '/emacs/{print $1}')
  do
      kill -USR1 $a
  done

And tell your window manager to run the script whenever you switch windows. I use sawfish and so:

  ;;.sawfish/rc code
  (defun save-emacs () 
    (system "save-emacs &"))
  (add-hook 'leave-workspace-hook save-emacs)

CategoryFiles