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.
eshell-mode-hook
local binding without the need for your function? Tobias' answer didn't work for me. I can't figure out how to passeshell/clear
the non-nil argument. When I try to useeshell/clear-scrollback
as the command, I get a "wrong type argument commandp" error.
– mcp Aug 19 '20 at 17:42(add-hook 'eshell-mode-hook (lambda () (local-set-key (kbd "C-l") 'eshell/clear-scrollback)))
clear
with the argument1
or calleshell/clear-scrollback
. The difference as I understand it: Sending input would mean entering the command as if it were typed in the shell. Calling the command would be equivalent toM-x clear 1
orM-x clear-scrollback
. – mcp Aug 20 '20 at 18:45M-x <function-name>
, e. g.,M-x magit
does the same thing asobla/di/da $ magit
in eshell. You can even doobla/di/da $ (message "Peace")
and it will display "Peace" in the mini-buffer. – scribe Aug 20 '20 at 23:32(add-hook 'eshell-mode-hook (lambda () (local-set-key (kbd "C-l") (eshell/clear 1))))
– scribe Aug 20 '20 at 23:35eshell/clear
function that needed writing your own? – user129393192 Apr 26 '23 at 06:21