I often find myself with an incomplete or invalid emacs config, which breaks the normal behavior of kill-emacs such that it stops at an error.

In this case, I would like to forcefully kill emacs, without resorting to hunting down the PID and issuing a kill from the shell. Can I do this in pure Emacs Lisp?

  • I'm not sure I follow. If kill-emacs doesn't work other elisp approaches may also be fouled up, as they probably end up calling kill-emacs one way or another. What error do you get? It may be easier to fix the error. – Tyler Dec 16 '14 at 20:37
  • I mean, you could create a function that calls kill -9 on the current Emacs pid for instance. You'd have to be very messed up to break that. – PythonNut Dec 16 '14 at 20:38
  • 5
    You can try setting kill-emacs-hook to nil before calling kill-emacs. You can also get Emacs's pid using emacs-pid and make it commit suicide by calling call-process with "kill -9". – Constantine Dec 16 '14 at 20:43
  • 1
    if you don"t want to hunt the PID, still, there is pkill & pgrep :\ – Nsukami _ Dec 16 '14 at 21:05
  • 1
    Unrelated, but you might want to avoid running multiple sessions of emacs – Sean Allred Dec 16 '14 at 21:11
up vote 13 down vote accepted

You can try setting kill-emacs-hook to nil before calling kill-emacs:

(defun really-kill-emacs ()
  "Like `kill-emacs', but ignores `kill-emacs-hook'."
  (interactive)
  (let (kill-emacs-hook)
    (kill-emacs)))

Alternatively, you can get Emacs's PID using emacs-pid and make it commit suicide by calling call-process with "kill -9":

(defun brutally-kill-emacs ()
  "Use `call-process' to send ourselves a KILL signal."
  (interactive)
  (call-process "kill" nil nil nil "-9" (number-to-string (emacs-pid))))
  • 3
    Not a critique of the answer, just additional information: note that using signal nine (the -9 in kill -9) will orphan any processes that emacs has spawned, just as it would happen if emacs were a simple shell script.. – Sean Allred Dec 16 '14 at 22:29
  • Although TERM often fails to kill emacs in a broken state. So it may be the only thing. – PythonNut Dec 16 '14 at 23:22

Sometimes, I end up with several unreachable emacs processes laying around. My emacsclient normally automatically starts an emacs and calls server-start--I guess there is some bug in my configuration somewhere which results in this happening more than once.

I kill the extra emacsen with killall emacs in a shell. So far, -9 hasn't been needed. (It works when there's only one emacs running, too, and I don't have to hunt down a pid!)

  • Sometimes, when emacs hangs completely, a -9 is needed. Of course, this means that emacs won't be able to commit suicide, but I'm adding it just in case. – PythonNut Dec 22 '14 at 19:30
  • This was the only thing that worked for me after an ein:jupyter-notebook was preventing all other methods. I guess is was hanging somewhere, but I had already ended all Python processes and killed all buffers. – n1k31t4 Nov 7 '17 at 8:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.