Is there any easy way to flush all output in the shell buffer? Since recently (I believe Emacs 24.4 already has this) *shell* buffers aren't editable, they behave similar to how typical terminal would behave, i.e. only allowing you to append text at the end, or to flush the last output, but you can't add text at any place or remove part of it arbitrarily. It feels like I'm missing something important about this improvement (?). It actually makes it more annoying. I searched the docs for the *shell* and *Messages* buffers, but couldn't find a mention of the question.

In addition to the original question: what's the point of this change? To me this doesn't feel like an improvement at all, so, unless there's "a catch", I'd like to set it to read-write as default.

I'm talking about Emacs 24.4, of course.

  • 2
    You seem upset about some "improvement", but you've failed to mention what it is. From your tag I infer you're referring to Messages being read-only now, but I don't see how that relates to shell buffers.
    – Malabarba
    Nov 28 '14 at 23:17
  • 1
    @Malabarba shell buffer used to be completely editable too. I.e. you could delete or add text to it at any point you liked, while now you can only append text at the end, and you can't remove it anywhere else too. *Messages* behaves in the same way.
    – wvxvw
    Nov 29 '14 at 9:22

(erase-buffer) won't work on comint and shell buffers if they are read-only.

Here's the defun I use quite often for clearing shellish buffers:

(defun clear-comint-buffer ()
  (let ((old-max comint-buffer-maximum-size))
    (setq comint-buffer-maximum-size 0)
    (setq comint-buffer-maximum-size old-max)
    (goto-char (point-max))))

You could perhaps bind this to s-k in your relevant modes to mimic the clearing behavior of OSX's Terminal.app.

Update 11/2016

Somewhere in Emacs 25 development (and also in my latest build which is ""), (comint-clear-buffer) was added to comint-mode, which is a more concise version of my custom defun. Here's the source for reference:

(defun comint-clear-buffer ()
  "Clear the comint buffer."
  (let ((comint-buffer-maximum-size 0))

This built-in solution also works if you have (setq comint-prompt-read-only t).

  • Bah, so this is by design... well, then this answers the question. Seems like I'll have to look for alternatives to M-x shell then :S
    – wvxvw
    Nov 29 '14 at 9:28
  • @wvxvw M-x erase-buffer works perfectly in a Shell Mode buffer in Emacs trunk, and I see no reason why it shouldn't work in 24.4 as well.
    – user227
    Nov 29 '14 at 10:14
  • @wvxvw I'm confused. My shell buffer is not read-only, and erase-buffer works fine on it. Maybe it's been changed back in Emacs-25, I'll try to check later.
    – Malabarba
    Nov 29 '14 at 10:38
  • @Malabarba I tried, and it works perfectly.
    – user227
    Nov 29 '14 at 12:08
  • 2
    @Malabarba / @lunaryorn - I found the reason why (erase-buffer) doesn't work for me personally. I have (setq comint-prompt-read-only t) in my config, which I find useful for not clobbering the prompt and thus (erase-buffer) barks at me.
    – waymondo
    Nov 29 '14 at 14:44

You can clean up the *Messages* buffer the same way you always could, with the kill-buffer command. The next time a message is signaled, the buffer will be recreated anew.

The reason that this buffer is read-only now, is that it has been granted its own major-mode, which inherits from special-mode, which is read-only. The advantages behind that are several. special-mode is designed for reading, so it has several keybindings that are very convenient for buffers that are not meant to be edited.

I'm not sure what *shell* has to do with anything. But if you want to erase it, M-x erase-buffer should do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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