LetWith the default settings of eshell
, typing the word "exit" in the *eshell*
buffer, followed by the enter key, causes Emacs to execute the function eshell/exit
. As such, the proposed approach set forth in the question above, i.e., by inserting the word "exit" and calling eshell-send-input
, is probably not the best way to handle this.
So, let us start by examining the built-in function eshell/exit
, which is essentially a one-liner. To do that, we evaluate (require 'eshell)
with something like M-:
(aka M-x eval-expression
) ; or, we could copy/paste the (require 'eshell)
statement to a *scratch*
buffer and place our cursor just after the closing parenthesis and type C-x C-e
. Then, we locate the function with M-x find-function RET eshell/exit RET
-- it looks like this:
(defun eshell/exit ()
"Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
(throw 'eshell-terminal t))
Since it is just a one-liner, why not create our own function by simply changing the function name, like so?:
(defun eshell/x ()
"Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
(throw 'eshell-terminal t))
We decide to pay attention to the doc-string about the variable eshell-kill-on-exit
and if we are curious, we type C-h v
(aka M-x describe-variable
) and look it up:
eshell-kill-on-exit is a variable defined in ‘esh-mode.el’.
Its value is t
Documentation:
If non-nil, kill the Eshell buffer on the ‘exit’ command.
Otherwise, the buffer will simply be buried.
You can customize this variable.
As luck would have it, we have the benefit of a similar thread which has an accepted answer:
https://stackoverflow.com/questions/51867693/emacs-eshell-kill-window-on-exit
So, we take the answer in that similar thread -- suggesting that we advise eshell-life-is-too-much
, and we combine it with our new function eshell/x
:
(require 'eshell)
(defun eshell/x ()
(throw 'eshell-terminal t))
(defun my-custom-func ()
(when (not (one-window-p))
(delete-window)))
(advice-add 'eshell-life-is-too-much :after 'my-custom-func)