In Emacs interactive subshell, when I type clear, the screen is not cleared as it should do in a normal Ubuntu Terminal, for example.

When I press Ctrl + l(twice if the screen is full), the screen gets cleared; but as soon as I type any command, let us say ls, I find myself in the bottom of the page (buffer) meaning that the clearing action is undone.

Is there a way to clear a subshell screen?

up vote 3 down vote accepted

You can clear the shell buffer like any other, if you get the cursor away from the current prompt, where its movement behavior is different. Here's one recipe, which feels intuitive to me but looks long when I write it down.

  • <Enter> to get a fresh prompt
  • C-b C-a moves the cursor to the beginning of the current line, to the left of your prompt
  • M-< to execute beginning-of-buffer
  • C-x C-x to execute exchange-point-and-mark, highlighting (nearly) everything in the buffer
  • C-w to execute kill-region

This gives you a shell buffer containing only (the final line of) the empty prompt you made at the start; you can put the cursor back in the usual place with C-e.

You can also trim outputs partially by using C-c C-n and C-c C-p to move through your command history from prompt to prompt, using C-<Space> to toggle the mark while moving and selectively highlight command output you no longer need, and C-w to kill highlighted regions. This is convenient if you have some command history that you mostly want to keep, but you've accidentally run some verbose command whose output you're no longer interested in. More flexible than killing your entire history.

Unlike in console shells, your current method of C-l C-l isn't actually clearing anything; it's running recenter-top-bottom twice, which you can do in any shell or non-shell Emacs buffer.

eshell-postoutput-scroll-to-bottom in the hook eshell-output-filter-functions is causing to scroll to bottom and so to remove it from the hook add this snippet to emacs config

(add-hook 'eshell-mode-hook
          (defun rm-eshell-postoutput-scroll-to-bottom ()
            (remove-hook 'eshell-output-filter-functions
                         'eshell-postoutput-scroll-to-bottom)))

after this you can use C-l C-l to clear screen in eshell and proceed with any commands without scroll

For me always works using C-x C-h to mark everything in the current buffer, then if the selection delete is enabled I just press enter/return. If it's not enabled, I add a backspace step before enter/return.

  • Do you have C-x C-h bound to mark-whole-buffer in some configuration file? It seems to be unbound by default. – rob Oct 11 at 18:47
  • @rob Windows and GNU/Linux GUI versions with non-english keyboard layout, nothing else. Weird. – KeyWeeUsr Oct 11 at 19:37

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.