I launch a shell in my session with M-x shell and then I launch a python script that produces a lot of output to stdout---so many lines of output that my emacs session beomes quite slow.

Is there a command I can run to ask my buffer to be fixed length and automatically delete lines older than N? I have tried looking this up, but don't even know what terms to use in my query beyond "automatically delete," "circular buffer" and "fixed length" which were not helpful.

  • 1
    You are looking for M-x comint-truncate-buffer. Ref: stackoverflow.com/questions/25862743/… Jul 20 '20 at 5:46
  • No, this doesn't do what I want. The buffer grows beyond 1024 soon after doing this. I'm looking for a fixed length not greater than X, not a one-off chop to that length. Jul 20 '20 at 5:53
comint-buffer-maximum-size is a variable defined in ‘comint.el’.
Its value is 1024

The maximum size in lines for Comint buffers.
Comint buffers are truncated from the top to be no greater than this number, if
the function ‘comint-truncate-buffer’ is on ‘comint-output-filter-functions’.

You can customize this variable.
(defun my-shell-mode-hook ()
  "Custom `shell-mode' behaviours."
  (add-hook 'comint-output-filter-functions 'comint-truncate-buffer nil :local))

(add-hook 'shell-mode-hook 'my-shell-mode-hook)
  • I have this defined and when used this, with the above, it clipped my buffer to 1024 lines. However, as the script kept running, it then proceeded to grow without bound. I can M-x comint-truncate-buffer again, manually, but this does not seem to fix the length at 1024 automatically as the buffer grows. Jul 20 '20 at 23:32
  • You would need to create a new shell buffer for shell-mode-hook to be called. You could use M-: (my-shell-mode-hook) RET in an existing buffer to set that config. Either way, C-h v comint-output-filter-functions in the shell buffer should include comint-truncate-buffer. Can you confirm whether you still have the problem after you've verified that the output filter is present for the shell buffer in question?
    – phils
    Jul 20 '20 at 23:47

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.