Running clear with 1, as in clear 1
works.

How could you have found this out? We know we want to look for a function about eshell and it has to do with clear. So, why not do C-h f
and type eshell clear
. This produces two functions:
eshell/clear-scrollback
eshell/clear
If we select eshell-scrollback
, Emacs shows us usage:
eshell/clear-scrollback is a compiled Lisp function in ‘esh-mode.el’.
(eshell/clear-scrollback)
Clear the scrollback content of the eshell window.
We try running eshell/clear-scrollback
and it clears the screen. Ok, we can look at it's implementation but before that let's look at the other function eshell/clear
. When we select that after C-h f
and eshell clear
, we get:
eshell/clear is an interactive compiled Lisp function in
‘esh-mode.el’.
(eshell/clear &optional SCROLLBACK)
Scroll contents of eshell window out of sight, leaving a blank window.
If SCROLLBACK is non-nil, clear the scrollback contents.
This tells us that we could run clear 1
to clear the screen. But, why are there two functions? If we look at the implementation of eshell/clear
, we can see that the other function is actually used in it. To find the implementation simply click on the esh-mode.el
in the usage buffer of eshell/clear
.
(defun eshell/clear (&optional scrollback)
"Scroll contents of eshell window out of sight, leaving a blank window.
If SCROLLBACK is non-nil, clear the scrollback contents."
(interactive)
(if scrollback
(eshell/clear-scrollback)
(insert (make-string (window-size) ?\n))
(eshell-send-input)))
You can see that at the end this function sends any input which was present at the time of clearing the screen. To get around it, I wrote this little function:
(defun run-this-in-eshell (cmd)
"Runs the command 'cmd' in eshell."
(with-current-buffer "*eshell*"
(end-of-buffer)
(eshell-kill-input)
(message (concat "Running in Eshell: " cmd))
(insert cmd)
(eshell-send-input)
(end-of-buffer)
(eshell-bol)
(yank)))
The benefits of this function are that if you put it in a lambda function and bind it to a key like this:
(bind-keys*
("C-<backspace>" . (lambda () ; clear shell
(interactive)
(run-this-in-eshell "clear 1"))))
It will first kill the current input, run clear 1
and then yank the input back. This way you can also clear eshell from any other buffer using the keybinding.
Some other functions provided will wipe out any buffer they are ran in. So if you used the keybinding to clear eshell from some other buffer, it will instead clear THAT buffer.