Prompting on Exit from Emacs

Coffeecupp_ over at the Emacs reddit asks if there’s a way on macOS to prevent Emacs from quitting when the last window is closed. That’s not a problem I have but I learned a couple of useful things from the discussion.

First, and this applies to all OSes, mbork suggests just setting the confirm-kill-emacs variable:

(setq confirm-kill-emacs #'y-or-n-p) ; or yes-or-no-p is you're really paranoid

For some technical reasons that doesn’t do quite what coffeecupp_ needed but it did solve a problem for me. I have a periodic task in which I need to take data from a Notes app window into Emacs. I do that be positioning my Emacs window over the Notes window so just the data I’m interested in shows. Afterwards, I usually use Cmd+Tab to bring the Notes app to the foreground so I can kill it with Cmd+q. Of course, I usually type Cmd+q first and kill Emacs instead.

I was sure that Emacs had a way to ask for confirmation before killing but I never got around to finding out what it is. Thanks to mbork, I now know so I won’t have that particular problem again. Normally, I hate confirmation messages like that but I kill Emacs on purpose much less frequently than I do by accident so it make sense for my particular workflow.

The other thing I learned is the difference between emacs –daemon and starting server mode in init.el. I always assumed they did pretty much the same thing but it turns out there’s a useful difference. If your goal is to ensure that Emacs keeps running—as opposed to just enabling emacsclient—then you should use emacs --daemon. The answers in the above Stack Overflow question explain this in a little more detail.

So, not bad for a question I didn’t care about in the first place.

UPDATE [2017-03-16 Thu 13:23]: y-or-no-p → y-or-n-p.

This entry was posted in General and tagged . Bookmark the permalink.
  • I've been using terminal emacs with byobu on osx. When I kill iterm2 by mistake or close my emacs tab, it is no big deal b/c byobu kept the session by way of tmux!

  • Martin Gausby

    I've solved the problem by remapping the `C-x C-c` to `delete-frame`, which will prompt «Attempt to delete the sole visible or iconified frame» if the last frame is about to get killed—to actually kill emacs I've bound `C-x C-C` (uppercased C) to `save-buffers-kill-terminal`.

    Works great for me. I never quit Emacs on accident.

    (define-key ctl-x-map (kbd "C-S-c") 'save-buffers-kill-terminal)
    (define-key ctl-x-map (kbd "C-c") 'delete-frame)