9

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, 25.0.50.1 of course.

wvxvw
  • 11,222
  • 2
  • 30
  • 55
  • 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 09:22

2 Answers2

17

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.

Malabarba
  • 22,878
  • 6
  • 78
  • 163
5

(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 ()
  (interactive)
  (let ((old-max comint-buffer-maximum-size))
    (setq comint-buffer-maximum-size 0)
    (comint-truncate-buffer)
    (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 "26.0.50.1"), (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."
  (interactive)
  (let ((comint-buffer-maximum-size 0))
    (comint-truncate-buffer)))

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

waymondo
  • 1,384
  • 11
  • 16
  • 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 09: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. –  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. –  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
  • I also like that defun because you can use it on a purposefully dedicated read-only `comint-mode` derived buffer, like a dedicated server log buffer in that is set to read-only. – waymondo Nov 29 '14 at 14:47
  • @Malabarba Ah, sorry, I've been using 25.0.50 version, but assumed it was the 24.4. – wvxvw Nov 29 '14 at 18:13
  • @waymondo well, now it's getting crazier :) I've tried `(setq comint-prompt-read-only nil)` and it still won't let me modify it. Well, hopefully it's just me being unlucky with this particular build, I might update next week and see if it's still there. – wvxvw Nov 29 '14 at 18:17