I activated setq-local confirm-kill-emacs (quote y-or-n-p) on a hook for a local mode which has a shortcut very similar to C-x C-c, in order to avoid accidentally closing Emacs when trying to execute the said shortcut. That way, Emacs will ask me to confirm it and I will be able to cancel it.

But this has an unfortunate consequence, that is, when I click on the close button on the top of Emacs' window, it also asks the same question. This is often annoying.

Is there a way to only ask for confirmation when using the keyboard shortcut, but not the mouse click?

1 Answer 1


you may define a separate command for C-x C-c like:

(defun keyboard-kill-emacs ()
  (let ((confirm-kill-emacs 'yes-on-no-p)) ; temporarily enable the confirmation

(global-set-key (kbd "C-x C-c") 'keyboard-kill-emacs)
  • Thanks, i'd edit it to fix the typo (yes-or-no-p) but it's below the 6-character minimum edit. Also, for lazy people, I'd add the code to put it as a hook to a local mode: (add-hook 'whatever-mode-hook (lambda () (local-set-key (kbd "C-x C-c") 'keyboard-kill-emacs))) In any case, it worked just as intended, thanks.
    – anol
    Feb 22, 2016 at 14:18

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.