1

I would like a way to make emacs to return a non-zero exit code when exiting.

This page https://www.gnu.org/software/emacs/manual/html_node/elisp/Killing-Emacs.html lists the command

kill-emacs &optional exit-data

as a mechanism for making emacs return with non-zero return code, stating "If exit-data is an integer, that is used as the exit status of the Emacs process".

How do I add this optional exit-data?

I have tried:

  • Interactively using M-x kill-emacs but cannot type a space after that.
  • Setting up a binding in my init.el using (global-set-key (kbd "C-x C-M-c") 'kill-emacs), but do not know the syntax well enough to pass an argument.

I see this is related to How to set the exit status for emacsclient and How to make emacs or emacsclient exit with nonzero return code? , but I cannot get the answer I want from those questions/answers.

Please can someone explain how to quit emacs with a non-zero return code and/or provide the syntax to setup a keyboard binding.

  • I was going to suggest looking at the doc string of the function with C-h f kill-emacs RET which should tell how to pass arguments to the function interactively, but in this case, that is not mentioned, perhaps assuming that this is "common knowledge". I wonder if a bug report on the doc string is warranted. – NickD Jul 16 at 12:38
2

Interactively, you can use a prefix argument.

E.g.:

  • $ emacs -Q
  • M-7 M-x kill-emacs RET
  • $ echo $?
    => 7

A command to do this, which you could bind to a key sequence, would be:

(defun my-kill-emacs ()
  (interactive)
  "Exit emacs with status 7."
  (kill-emacs 7))
| improve this answer | |
  • Works perfectly. I've accepted the answer. Thanks! – ianinini Jul 16 at 12:11

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.