(NB: In this post, I will use expressions like "a buffer's underlying file" to mean, in the Emacs' documentation's terminology, "the file a buffer is visiting", i.e. the value of (buffer-file-name SOME-BUFFER) for the buffer SOME-BUFFER.)

Sometimes it happens that a buffer's underlying file gets modified on disk by some other process.

If one tries to modify such a buffer, Emacs warns of the on-disk change, and requests confirmation; e.g. (in the minibuffer):

one_really_busy_file changed on disk; really edit the buffer? (y, n, r or C-h) 

Furthermore, if after responding y to such a confirmation request, one modifies such a buffer, and then attempts to save the modified buffer, then Emacs once again issues a warning and a confirmation request; e.g.:

one_really_busy_file has changed since visited or saved.  Save anyway? (y or n)

To be sure, Emacs does not regard all modifications to a buffer's underlying file as being equally worthy of all this fuss. Changes to the underlying file's permissions, for example, do not elicit any of this.

Strangely enough, according to out-of-the-box Emacs, a buffer's underlying file's disappearance also fails to warrant such a response!

To be more specific, Emacs remains perfectly quiet if one modifies a buffer whose underlying file has disappeared, and—even more surprising—, it remains equally silent when one then saves (C-x C-s) the modified buffer (thereby re-creating the underlying file).

Q: How can I get Emacs to issue a warning and a confirmation request also when one attempts to modify a buffer whose underlying file has disappeared?


2 Answers 2


You can do this:

(add-hook 'before-save-hook 'foo)

(defun foo ()
  (when (and (buffer-file-name)  (not (file-exists-p (buffer-file-name))))
    (unless (y-or-n-p "No such file.  Create it?")
      (error "OK, not saved"))))

However, this could be regarded as a feature: Emacs creates a file for a buffer you ask it to save.

But if you don't like this behavior you might consider filing a bug report (enhancement request): M-x report-emacs-bug.

  • Thank you! To be clear, the condition described in my post is the one corresponding to (and (buffer-file-name) (not (file-exists-p (buffer-file-name)))), whereas the one in your function is (not (and (buffer-file-name) (file-exists-p (buffer-file-name)))); they are not equivalent. Also, I would like a similar warning+confirmation right before I attempt to modify a buffer whose underlying file does not exist. To extend your idea to do this would require a hook that would get triggered when one attempts to modify a buffer. Is there such a hook?
    – kjo
    Dec 2, 2021 at 21:17
  • 1
    Oops. I corrected the condition.
    – Drew
    Dec 2, 2021 at 22:08
  • Please don't add questions to your question (including in comments). One question per post, please.
    – Drew
    Dec 2, 2021 at 22:09
  • 1
    My "added" question was already in my original post: I wrote (emphasis added) "How can I get Emacs to issue a warning and a confirmation request also when one attempts to modify a buffer whose underlying file has disappeared?"
    – kjo
    Dec 2, 2021 at 22:52
  • 1
    @Drew, I think you mean write-file-functions. before-save-hook functions are not checked for success/failure.
    – rpluim
    Dec 3, 2021 at 11:21

You can use the write-file-functions hook to do this. It's what's called an "abnormal" hook, which means its return value is taken into account, unlike before-save-hook. In this case, if it returns t, the `save-buffer' command returns without saving the buffer. E.g.

(add-hook 'write-file-functions #'foo)

(defun foo ()
  (when (and (buffer-file-name) (not (file-exists-p (buffer-file-name))))
    (not (y-or-n-p "No such file.  Create it?"))))

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.