Emacs eshell - Kill window on exit

909 views Asked by At

I have the following code in my init.el

;;open eshell
(defun eshell-other-window ()
  (interactive)
  (let ((buf (eshell)))
    (switch-to-buffer (other-buffer buf))
    (switch-to-buffer-other-window buf)
  )
)
(global-set-key (kbd "C-t") 'eshell-other-window)

This works fine until I exit eshell. When I exit, the window stays open. How do I get the window to close automatically?

2

There are 2 answers

0
lawlist On BEST ANSWER

The following answer assumes that the user is typing exit at the command prompt in the *Eshell* buffer followed by the return/enter key, and the answer assumes that the function eshell/exit is doing its thing. [The user is still free to customize the variable eshell-kill-on-exit to either burry or kill the *Eshell* buffer when exiting.]

(require 'eshell)

(defun my-custom-func () 
  (when (not (one-window-p))
    (delete-window)))

(advice-add 'eshell-life-is-too-much :after 'my-custom-func)
0
Mehrad Mahmoudian On

Building on what @lawlist wrote back in 2018, and as i could not make it work using use-package, this is the solution I ended up using:

(use-package eshell
  :ensure nil
  :init
  (defun mm/kill-window-if-no-last ()
    "Checks if this window is NOT the last window, it will kill this window"
    (when (not (one-window-p))
      (delete-window)))

  :hook (
         (eshell-exit . mm/kill-window-if-no-last)
         )
  )

Please note that my mm/kill-window-if-no-last function is practically the my-custom-func in the @lawlist answer, just with a more descriptive name and also some description.