4

If I am closing a file that has modifications, emacs warns me about this and says I should press y or n.

Even when I press 'n', it still tells me that modified buffers exist and asks if I want to save anyway.

Is there a way I can stop this second 'modified buffers exist' message from appearing? Once I have pressed 'n', it should be clear to emacs that I do not want to save.

I do want it to ask me the first 'y or n' question, though.

2

1 Answer 1

1

This warning is built into save-buffers-kill-emacs. You can't disable it easily.

A simple way to get rid of it is to tell Emacs to save all buffers, by passing a prefix argument to the save-buffers-kill-emacs command (or save-buffers-kill-terminal which calls it): type C-u C-x C-c instead of C-x C-c. However you may not want to do that.

You can use the advice facility to modify the behavior of functions. Here, it's cumbersome because the code calls a generic function (yes-or-no-p) right in the middle of some logic that's hard to hack.

(require 'cl)
(defadvice save-buffers-kill-emacs
  (around save-buffers-kill-emacs-no-modified-yes-no activate compile))
  (let ((real-yes-or-no-p (symbol-function 'yes-or-no-p)))
    (flet ((yes-or-no-p (msg)
             (or (equal msg "Modified buffers exist; exit anyway? ")
                 (funcall real-yes-or-no-p msg)))))
    ad-do-it))

You may prefer to copy the definition and remove the offending call, however this is likely to require more maintenance as Emacs evolves.


Do you really need this? First, warning of unsaved changes is usually a good idea, which is why Emacs does it. Second, it's typical to start Emacs with your session and exit it when you log out, so exiting Emacs is something that happens rarely.

5
  • 3
    I agree that warning of unsave changes is good, and emacs is already doing this when it asks the first time if I want to save changes. I don't need it asking the second time, too. Jan 8, 2014 at 3:38
  • The code snippet doesn't work. I tried the following corrections: removed a ) bracket after activate compile)), changed flet to cl-flet. Still did not work. Apr 6, 2020 at 13:23
  • @run_the_race I think this worked (without the stray parenthesis) in earlier versions of Emacs that didn't use lexical bindings. Lexical binding makes such monkey patching harder. Apr 6, 2020 at 13:46
  • Found this works for Emacs 26.3: stackoverflow.com/a/6764804/5506400 Apr 6, 2020 at 15:09
  • @run_the_race Beware that this removes quite a lot of stuff, such as prompting if there are active processes, or any package that registers a confirmation prompt at Emacs exit. Apr 6, 2020 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .