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:
If we select
eshell-scrollback, Emacs shows us usage:
eshell/clear-scrollback is a compiled Lisp function in ‘esh-mode.el’.
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
(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
(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."
(insert (make-string (window-size) ?\n))
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."
The benefits of this function are that if you put it in a lambda function and bind it to a key like this:
("C-<backspace>" . (lambda () ; clear shell
(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.