14

I use Emacs Dired to manage files. Accidentally, I hit Enter on an image file, Emacs tries to open the image and stop response. Is there a way to kill this process?

(Since I am running other processes at the same time, I don't want to restart Emacs.)

Edit: Is there a way to list all the processes within Emacs and kill a certain one like in shell: kill -9 process-id?

Edit 2: Although very slow, I managed to press C-x C-b to list all the buffers, and d mark the buffer caused Emacs no-response, x to execute delete. It succeed.

This only works as long as Emacs could response to my key. I really don't know what to do if I can't list all the buffers.

Nick
  • 4,423
  • 4
  • 24
  • 41

2 Answers2

12

Press C-g repeatably. The first time might be ignored, the second time it should do the trick. If that fails also try pressing ESC three times.

tarsius
  • 25,298
  • 4
  • 69
  • 109
12

I've personally experienced hangs that cannot be broken by any number of C-g presses.

A more forceful method on UNIX OSs is to send Emacs a USR2 signal. Signal processing is a lot lower-level than keyboard input, and has a very good chance of unhanging Emacs. (I've never seen it fail)

$ killall -USR2 emacs 

Should do the trick. In addition USR2 creates a backtrace. If you don't want that, you might have some luck with the following (I haven't tested this, though):

(defun quit-on-usr2 ()
  (interactive)
  (keyboard-quit))
(global-set-key [signal usr2] #'quit-on-usr2)
PythonNut
  • 10,243
  • 2
  • 29
  • 75
  • This is quite old, but just came across it. Can you explain the [signal usr2] part. I haven't seen that as a keystroke before. – Joe Jan 04 '22 at 16:30
  • Never mind: Thank you for this, this allowed me to construct a useful technique to stop a runaway Emacs timer when C-g doesn't respond. For others, I think you got this from: https://www.gnu.org/software/emacs/manual/html_node/elisp/Event-Examples.html#Event-Examples – Joe Jan 04 '22 at 17:41
  • Your answer gave birth to this, which works really well for me: https://emacs.stackexchange.com/questions/506/debugging-a-frozen-emacs/70000#70000 – Joe Jan 04 '22 at 18:18