# How to clear the eshell

Emacs Asked by Startec on December 24, 2020

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

I use make eshell buffers and I rename them accordingly. So a slight modification to @scribe's answer works perfectly on eshell buffers with anyname.

(defun run-this-in-eshell (cmd)
"Runs the command 'cmd' in eshell."
(end-of-buffer)
(eshell-kill-input)
(message (concat "Running in Eshell: " cmd))
(insert cmd)
(eshell-send-input)
(end-of-buffer)
(eshell-bol)
(yank))

(interactive)
(local-set-key (kbd "C-l")
(run-this-in-eshell "clear 1"))))


Answered by soumya ranjan Tripathy on December 24, 2020

The doc string of eshell/clear says:

(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.

So call clear t on the Eshell prompt to clear all Eshell buffer contents.

For binding of the command to a key you can use something like the following in eshell-mode-hook:

(local-set-key (kbd "C-<backspace>") (lambda () (interactive) (eshell/clear 1)))


This answer is similar to scribe's answer and I initially wanted just to comment that one. But, then I thought that this answer goes a bit further. It also gives you a hint on how to use Eshell commands and how to discover their comments yourself.

Answered by Tobias on December 24, 2020

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:

1. eshell/clear-scrollback
2. 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.

Answered by scribe on December 24, 2020

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)))


Answered by Tianxiang Xiong on December 24, 2020

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.

Answered by K.D.G on December 24, 2020

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.

## Related Questions

### How to measure the performance of the mode-line?

1  Asked on December 7, 2021

### elfeed + olivetti modes

1  Asked on December 7, 2021 by luis-silva

### Restrict available command options in AuCTeX

1  Asked on December 5, 2021

### How to automatically remove a hook provided by a minor mode after disabling that mode?

1  Asked on December 5, 2021 by caseneuve

### SVG image display blurry

1  Asked on December 4, 2021

### Autoloaded variable overrides the one from the init file

1  Asked on December 4, 2021

### How to use keyword symbols in Emacs Lisp?

3  Asked on December 4, 2021

### How do I configure helm-git-grep candidates limit?

1  Asked on December 2, 2021 by wawrzyniec-pruski

2  Asked on November 30, 2021 by mmmmmm

### File within a root grandparent is detected by emacs as belonging to a directory that doesn’t exists

1  Asked on November 30, 2021

1  Asked on November 30, 2021

### eww browser is hanging at contacting duckduckgo

1  Asked on November 25, 2021 by cryptograthor

### How to move by defun without moving up a level?

0  Asked on November 25, 2021 by cammil

### Reset custom variable to default value programmatically

3  Asked on November 22, 2021 by itsjeyd

### How to make emacs automatically spell check all visible text?

2  Asked on November 22, 2021

2  Asked on November 20, 2021 by vince-w

### Symbol’s function definition is void

1  Asked on November 20, 2021 by 546756ryd

### How to pretty-format code (auto-insert newlines, indent, etc)?

7  Asked on November 18, 2021 by emmanuel-touzery

### org-latex-fragment gets cutoff

1  Asked on November 17, 2021 by quarky-quanta

### Display #+INCLUDE File Contents in Github README.org?

2  Asked on November 15, 2021 by matthew-zeng