Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to clear shell while using emacs shell

Tags:

shell

emacs

elisp

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

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

like image 558
keeda Avatar asked Oct 11 '11 23:10

keeda


People also ask

How do I close Emacs shell?

To get back into Emacs, type %emacs at the shell prompt. To quit Emacs permanently, type C-x C-c.

How do I run a shell command in Emacs?

You can execute an external shell command from within Emacs using ` M-! ' ( 'shell-command' ). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size.


2 Answers

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:

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

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

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

  1. 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))) 
like image 84
Tyler Avatar answered Oct 05 '22 10:10

Tyler


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.

like image 32
antonio Avatar answered Oct 05 '22 11:10

antonio