1

With auto revert enabled, is there any way to save a backup?;)

, Justin Case

I've looked in the docs, but I find no reference to saving a snapshot before it sends the bits off to /dev/null

3
  • You want a kind of autosave of the buffer contents before they are reverted to match the file on disk? I think you'll need to write some advice for auto-revert-handler to do that. (Regarding terminology, "backups" in Emacs are about copying or renaming the file on disk before overwriting it with modified contents, whereas "autosaves" are about writing buffer contents to disk even when you didn't explicitly say to do so. Your requirement seems more like the latter than the former.)
    – phils
    Mar 13, 2023 at 0:02
  • A side note: auto-revert mode doesn't revert files if the buffer is modified. This will prevent most scenarios where a file is auto-reverted accidentally. Mar 14, 2023 at 11:47
  • Also of note is that (since Emacs 24, I think) you can undo a revert-buffer (subject to general undo constraints).
    – phils
    Mar 4 at 8:10

1 Answer 1

1

The relevant parts of my init file below -- inspired by a disastrous git-filter-repo blunder that led me here. So far in testing using external file reversions via git checkout HEAD -- [file] these create an auto-save version of a file buffer's last state before the reversion occurs.

;; keep a safety net should a saved buffer get its disk file reverted
(setq delete-auto-save-files nil)

;; Before a buffer is reverted make a final auto-save
(add-hook 'before-revert-hook
          (lambda ()
            (write-region (point-min) (point-max) (make-auto-save-file-name))))

;; Normally I do want to revert buffers if their file changed on disk
(global-auto-revert-mode 1)

Possibly relevant: I also manage that my backups and save files end up in one place:

(let* ((my-backup-dir (expand-file-name "~/Downloads/EmacsBackups")))
  (make-directory my-backup-dir t)
  (setq backup-directory-alist `((".*" . ,my-backup-dir))
        auto-save-file-name-transforms
        `((".*" ,(concat my-backup-dir "/") t))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.