How can I prevent Emacs from auto-reverting only when the file size is over 10 GB? I know that there's the large-file-warning-threshold option but it doesn't work for auto-reverting.

I'm dealing with a file that I frequently overwrite programmatically. It's a small file most of the time, but it sometimes becomes unexpectedly large so I'd like to prevent Emacs from freezing when I leave that file open.

  • Good question. If you do not get an acceptable answer here, consider filing an Emacs enhancement request: M-x report-emacs-bug.
    – Drew
    Dec 26 '16 at 18:12

Assuming you're not using auto-revert-tail-mode, you might be able to just tell Emacs that if the file is too large, then the buffer is not "stale". E.g. something like:

(defun my-not-too-big (&rest _)
  (or (not buffer-file-name)
      (let ((size (nth 7 (file-attributes buffer-file-name))))
        (< size 10000000000))))
(add-function :after-while buffer-stale-function

Rather than doing that globally, you could use (add-function :after-while (local buffer-stale-function) #'my-not-too-big) in a mode hook to only do that in the specific buffers you care about.


Maybe use add-file-hook to turn off auto-revert-mode for a file buffer that is larger than some limit. This seems to work OK (tested minimally):

(defvar my-max-auto-revert-size 1000000000
  "Do not auto-revert file buffers larger than this.")

(add-hook 'find-file-hook
          (lambda ()
            (when (> (buffer-size) my-max-auto-revert-size)
              (setq global-auto-revert-ignore-buffer  t))))

(FWIW, I just added an enhancement request to make this kind of thing easier: Emacs bug #25277.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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