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.

Drew
  • 75,699
  • 9
  • 109
  • 225
ianinini
  • 113
  • 4
  • 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 '20 at 12:38

1 Answers1

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))
phils
  • 48,657
  • 3
  • 76
  • 115