2

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?

Tornado547
  • 231
  • 2
  • 10
  • 1
    How about using the function `delete-window`, which has an optional argument to specify a particular window if so desired? [See also `delete-other-windows`.] You can also use things like `select-window` to select a particular window, and you can use things like `set-window-buffer` to display a buffer in a particular window. And, you can get a window object with a function such as `selected-window` and/or `window-in-direction` with the arguments `'above`, `'below`, `'left`, `'right`. – lawlist Aug 15 '18 at 23:30
  • @lawlist yes, but how would I do that on eshell exit – Tornado547 Aug 15 '18 at 23:35
  • 1
    `kill-buffer` and `delete-window`, but check to see if more than one window exists. – lawlist Aug 15 '18 at 23:37
  • i understand i need to use those commands, but how do i execute when I run the `exit` command – Tornado547 Aug 15 '18 at 23:38

1 Answers1

4

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)
lawlist
  • 13,099
  • 3
  • 49
  • 158