25

How to disable Emacs from checking the buffer file was changed outside the editor?

msorc
  • 907
  • 1
  • 7
  • 20

4 Answers4

20

Emacs is really trying to help you here. Read the info page on Protection against Simultaneous Editing.

But, if you still want to avoid that message/prompt, you can redefine the function that is doing the prompting:

(defun ask-user-about-supersession-threat (fn)
  "blatantly ignore files that changed on disk"
  )
(defun ask-user-about-lock (file opponent)
  "always grab lock"
   t)

The second function there is for when two people are using Emacs to edit the same file, and would provide a similar prompt (but not the one you seemed to refer to in the question).

I'd advise against overriding the two routines, but it's there if you want.


On the off chance global-auto-revert-mode is on, you could disable that. Add this to your .emacs:
(global-auto-revert-mode -1)

You can tell if the mode is on by looking at the variable of the same name:

C-h v global-auto-revert-mode RET

If the value is t, then the mode is on, otherwise it is off.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 6
    The comes up a lot when running emacs in VMWare and editting files in /mnt/hgfs (links to host machine folders). VMWare's clock gets out of sync with the host -- occassionally, you can even see the result of time system calls jump backwards in time. So,... is it possible to disable the check only when editting files in a specific directory? (e.g. /mnt/hgfs?) – user48956 Apr 19 '13 at 18:04
  • 1
    This workaround is also needed when using Emacs on Windows through Parallels on OSX to modify files on the native/host OSX. In addition to modifying `ask-user-about-suppression-threat`, I also modified `basic-save-buffer` with a `defalias` and a new function that omits / removes:  `(or (verify-visited-file-modtime (current-buffer)) (not (file-exists-p buffer-file-name)) (yes-or-no-p (format "%s has changed since visited or saved. Save anyway? " (file-name-nondirectory buffer-file-name))) (user-error "Save not confirmed"))`. – lawlist Oct 21 '13 at 18:32
  • 1
    Emacs should really check that the contents of the file have changed and not just the timestamps. This happens on all my buffers every time I switch git branches temporarily and switch back (or git stash save/pop). *Edit* have just discovered @doublep 's answer – EoghanM Oct 20 '17 at 10:57
  • For those that have issues with VMWare `/mnt/hgfs`, have a look on this: https://stackoverflow.com/a/29556894/450148 – Felipe Oct 02 '18 at 03:58
11

In my case I wanted:

(setq revert-without-query '(".*"))

Documentation for revert-without-query:

Specify which files should be reverted without query.
The value is a list of regular expressions.
If the file name matches one of these regular expressions,
then ‘revert-buffer’ reverts the file without querying
if the file has changed on disk and you have not edited the buffer.
Resigned June 2023
  • 4,638
  • 3
  • 38
  • 49
10

I have the following in my .emacs. It makes Emacs only ask about really changed files. If a file remains the same bytewise, just its timestamp is updated, as often happens when you switch branches in VCS, this "change" is ignored by Emacs.

;; Ignore modification-time-only changes in files, i.e. ones that
;; don't really change the contents.  This happens often with
;; switching between different VC buffers.

(defun update-buffer-modtime-if-byte-identical ()
  (let* ((size      (buffer-size))
         (byte-size (position-bytes size))
         (filename  buffer-file-name))
    (when (and byte-size (<= size 1000000))
      (let* ((attributes (file-attributes filename))
             (file-size  (nth 7 attributes)))
        (when (and file-size
                   (= file-size byte-size)
                   (string= (buffer-substring-no-properties 1 (1+ size))
                            (with-temp-buffer
                              (insert-file-contents filename)
                              (buffer-string))))
          (set-visited-file-modtime (nth 5 attributes))
          t)))))

(defun verify-visited-file-modtime--ignore-byte-identical (original &optional buffer)
  (or (funcall original buffer)
      (with-current-buffer buffer
        (update-buffer-modtime-if-byte-identical))))
(advice-add 'verify-visited-file-modtime :around #'verify-visited-file-modtime--ignore-byte-identical)

(defun ask-user-about-supersession-threat--ignore-byte-identical (original &rest arguments)
  (unless (update-buffer-modtime-if-byte-identical)
    (apply original arguments)))
(advice-add 'ask-user-about-supersession-threat :around #'ask-user-about-supersession-threat--ignore-byte-identical)
  • +1 This is very similar to what I was thinking, but it will only work right if the buffer doesn't have unsaved changes. I was thinking I would maintain an assoc array of buffer SHA1s (or MD5 or whatever is cheap enough to calculate on the fly and robust enough against clashes) and update it whenever a new file is visited or whenever I save a file. Then if the file with the updated timestamp has the same hash, the alert is unnecessary. – tripleee Apr 10 '15 at 09:36
  • Yes, it's just as you described and has that unhandled case. But it's enough for me, because I have a habit of saving buffers often. –  Apr 10 '15 at 10:31
  • @doublep you are my hero! – EoghanM Oct 20 '17 at 10:59
  • 1
    This feature is now built in, starting with Emacs 26: [see the changelog](https://github.com/emacs-mirror/emacs/blob/master/etc/NEWS.26#L556) – Arkanosis Jun 18 '18 at 12:29
  • @Arkanosis could you be a little bit more explicit? Your link goes to line 556 in the document, but I can't see anything related to this there. I am running 26.1 and I still seem to have this issue. – martinweiss Aug 30 '18 at 08:37
  • 1
    @martinweiss : sorry I made the mistake to link to the master branch, which has changed in the meantime. Here's [the link to the relevant version](https://github.com/emacs-mirror/emacs/blob/7adf1a361db3a2e9b7a27d76946482276c24fb64/etc/NEWS.26#L556). I have compiled my own Emacs 27 on distros that ship older releases, and I haven't had this issue for months, whereas I had it every single morning before with Emacs 24 (some NFS issue, I guess). – Arkanosis Aug 30 '18 at 12:12
9

I had annoyance with this because every time I switched branches in git, emacs thought all my files had changed.

Revbuffs helps you cope with the symptoms of this. It allows you to cause all your buffers to be reloaded.

You can also try (global-auto-revert-mode) which will automatically revert your files to what's on disk.

Singletoned
  • 5,089
  • 3
  • 30
  • 32