When I press Ctrl-D in a 'term', the buffer stays open with the text "Process terminal finished".

How can I make this buffer close instead of staying open with this text displayed?

Note, this is similar to this question: How to automatically kill a shell buffer when the shell process exits however the solution there didn't work for 'term'.

  • Are you running background jobs?
    – waltinator
    Commented May 13, 2021 at 2:29
  • No, this happens with emacs -Q, run term (accept shell) then press Ctrl-D to exit it.
    – ideasman42
    Commented May 13, 2021 at 2:47
  • To test if a process is running, term.el itself uses: (let ((proc (get-buffer-process (current-buffer)) ...); finding the right hook is the hardest part. Does the sentinal stuff execute from the linked question about shell-mode, or not at all?
    – ctietze
    Commented May 13, 2021 at 10:31

1 Answer 1


Add your (kill-buffer (current-buffer)) to the tail end of term-handle-exit, either with an advice or just create a new function by the same name (ensuring that the library containing said function gets loaded before redefining it). To see what triggers it, have a look at term-sentinel. To verify it's the right place, just put in a few messages and see what happens when you type C-d in the term buffer.

(defun my-term-handle-exit (&optional process-name msg)
  (message "%s | %s" process-name msg)
  (kill-buffer (current-buffer)))

(advice-add 'term-handle-exit :after 'my-term-handle-exit)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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