不知道你有没有遇到过这样的问题,有时候会不小心按到 C-c C-x,造成 Emacs 意外退出,这个时候如果有个提示确认的动作就好了,通过配置 confirm-kill-emacs 就能够实现这一功能,关于 confirm-kill-emacs 的说明如下:
How to ask for confirmation when leaving Emacs.
If nil, the default, don’t ask at all. If the value is non-nil, it should
be a predicate function; for example ‘yes-or-no-p’.
不过如果直接把它设置成 yes-or-no-p 的话,那么每次退出都要按一下 y 才行,又有点麻烦,最后能设定一个时间,超过这个时间则默认自动退出,这个行为可以使用 y-or-n-p-timeout 来实现。
;; 为防止不小心按到C-c C-x,在退出Emacs前需要确认
(setq confirm-kill-emacs (lambda (prompt) (y-or-n-p-with-timeout "是否退出Emacs:(" 10 "y")))
这样,在按下 C-c C-x 后 Emacs 会提示你 是否退出 Emacs,这个时候按下 n 则不会退出 Emacs,若10秒内没有输入,则自动退出。

