3

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'.

ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • Are you running background jobs? – waltinator May 13 '21 at 02:29
  • No, this happens with `emacs -Q`, run `term` (accept shell) then press `Ctrl-D` to exit it. – ideasman42 May 13 '21 at 02: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 May 13 '21 at 10:31

1 Answers1

2

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)
lawlist
  • 18,826
  • 5
  • 37
  • 118