# How to clear the eshell

After looking at this answer I find it hard to believe there is no way to clear the eShell.

Does anybody know of a short function or method to delete the entire buffer. Pressing C-l just scrolls the text, as soon as enter is pressed all of that text comes back.

Thanks

There is a function in the current development version, as you can see on the emacs-devel mailing list.

The function is very simple:

(defun eshell/clear ()
"Clear the eshell buffer."
(erase-buffer)
(eshell-send-input)))


Typing clear in eshell will then result in clearing the buffer.

• oh wow. how will i know when this is available in a main release? – Startec May 19 '15 at 8:57
• You don't need to wait for the next release. You can just add this definition to your ~/.emacs.d/init.el file. You will know that it's available by checking for its definition with C-h f eshell/clear RET. – rekado May 19 '15 at 10:21
• I am using an older version of Emacs which did not require (eshell-send-input) -- that line of code created an extra command prompt. – lawlist Sep 28 '17 at 17:47

This does it to one, insted of two lines.

;Clear the eshell buffer.
(defun eshell/clear ()
(let ((eshell-buffer-maximum-lines 0)) (eshell-truncate-buffer)))


Typing clear in eshell will then result in clearing the buffer.

• Whoever left a demerit (downvote) without leaving a helpful constructive comment, please don't do that again -- especially when you see a newcomber to this forum! I am an intermediate Emacs user and I shouldn't have to sit here scratching my head to try and figure out why this answer received a downvote! It should not be necessary for me to try out the answer to maybe figure out why it was downvoted! – lawlist Sep 29 '17 at 5:23
• If you add (interactive) after the defun declaration, all works as expected. – Paradiesstaub Jan 3 '18 at 3:02
• Slightly better then default, clear, which doesn't seem to really clear. – Didier A. Aug 23 '18 at 5:14

While there is a built-in eshell/clear in recent versions of Emacs, it does something rather unfortunate: when there is unsent input at the current prompt, clearing the buffer sends the input. This can be problematic if the user has entered partial input.

The following version will clear the buffer while preserving unsent input. This is more comint-like.

(defun *-eshell-clear ()
"Clear eshell' buffer, comint-style."
(interactive)
(let ((input (eshell-get-old-input)))
(eshell/clear-scrollback)
(eshell-emit-prompt)
(insert input)))
`