ChatGPT解决这个技术问题 Extra ChatGPT

Command to clear shell while using emacs shell

Is there a builtin command to clear shell while using shell in emacs?

If not, is there an elisp function to achieve the same?


T
Tyler

Update February 2015

Just noticed that Emacs now (version 25+) has the command comint-clear-buffer, bound to C-c M-o by default, that does what we need here, and probably is preferable to the answers I originally posted below.

Options to consider:

C-l will recenter the buffer. Pressing it repeatedly cycles the buffer, so that point appears at the top, middle, or bottom of the buffer. When it stops at the top, the buffer looks like it's been cleared, although all the text is still there, out of view. C-x h marks the whole buffer, after which C-w kills it. This kills the last prompt as well, but after you enter the next command you get your prompt back. You can also use erase-buffer, which isn't bound to a key by default, but it's easily done (you can also use M-x erase-buffer:

    (defun my-shell-hook ()
      (local-set-key "\C-cl" 'erase-buffer))

    (add-hook 'shell-mode-hook 'my-shell-hook)

That binds it to C-c l; you can pick what you like.

A quick fix to re-create your prompt after clearing is possible:

    (defun my-clear ()
      (interactive)
      (erase-buffer)
      (comint-send-input))

    (defun my-shell-hook ()
      (local-set-key "\C-cl" 'my-clear))

    (add-hook 'shell-mode-hook 'my-shell-hook)

After you've been using emacs for a while, marking and killing regions becomes natural, so you might find the first option is enough. If not, the last option is closest to what you want.

EDIT: just found this on the emacs wiki, it's better than my option 4:

(defun my-clear ()
  (interactive)
  (let ((comint-buffer-maximum-size 0))
    (comint-truncate-buffer)))

In shell mode, C-c C-o does comint-delete-output, i.e. flushes the latest command's output. This is not what you were asking, but I am adding it here for completeness. C-c C-r might also be close to what the OP wants, even though it does not actually clear the shell buffer.
Thanks, missed that. I usually use either C-c C-o or C-space C-c C-p C-w, with the number of C-c C-p combinations varying depending on how many commands I want to clear.
just to point out that options 1,2,3 don't work for cases when the shell is a read only buffer; for example python inferior shell. In that case only C-c M-o works. Options 1 2 and 3 do work in M-x shell mode though, since that is not read only.
a
antonio

Most of the solutions proposed here will not work in EShell mode!

EShell mode buffer is read only, so kill and erase commands will not work.

To use your ordinary Ctrl-L to clear eshell terminal, add this to your .init file:

(defun eshell-clear-buffer ()
  "Clear terminal"
  (interactive)
  (let ((inhibit-read-only t))
    (erase-buffer)
    (eshell-send-input)))
(add-hook 'eshell-mode-hook
      '(lambda()
          (local-set-key (kbd "C-l") 'eshell-clear-buffer)))

Note: To better emulate the standard Ctrl-L, after clearing the buffer, the command will restore the initial prompt.


This is exactly what I needed
P
Paul

With your point in the *shell* buffer, run:

C-c M-o

or

M-x comint-clear-buffer

NOTE: This also works in *Python* shell, but doesn't seem to work in *eshell*.


J
Jesus Ramos

AFAIK you can't clear when you start as M-x shell but if you do M-x term you get a fully functional terminal where clear works as intended. I may be wrong on this though but personally I use M-x term now since it is fully functioning.


Thanks a lot. Now I can't stop but wonder why people use M-x shell instead of M-x term. Never mind you answered my question at the end.
For me, my zsh renders properly in shell and eshell but not in term. So that's the reason :)
A
Aborn Jiang

If you find, you can't use erase-buffer and shows text is read only. You can use following replace function.

  (defun eshell/clear ()
  "Hi, you will clear the eshell buffer."
  (interactive)
  (let ((inhibit-read-only t))
    (erase-buffer)
    (message "erase eshell buffer")))

u
user2762156

Or... you can truncate the buffer.

(let ((eshell-buffer-maximum-lines 0)) (eshell-truncate-buffer))