8

I currently truncate buffers at 20,000 lines by adding the following to my configuration:

(setq eshell-buffer-maximum-lines 20000)
;; automatically truncate buffer after output
(add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)

This works great for < 20,000 lines, and once the buffer reaches 20,000 lines it gets truncated on every line (so it sits at 20,001 lines forever). However, when the buffer reaches 20,000 lines, each subsequent line takes much longer to be printed and causes the CPU of emacs to sit at 100% for quite a while.

Is there a way to improve the performance of the buffer truncation? Perhaps only truncating every 100 added lines? It's not like I really need it to be exactly 20,000 lines, I just don't want the buffer to grow infinitely.

2
  • 3
    Why not truncate after idle-time?
    – PythonNut
    Apr 22, 2015 at 3:33
  • it gets truncated on every line because you run eshell-truncate-buffer each each time output is produced. It may make more sense to make truncation independent on producing output. As @PythonNut suggests truncating after timeout seems more reasonable.
    – user2005
    Apr 22, 2015 at 8:43

1 Answer 1

7

Taking the suggestion from PythonNut and rekado, I have switched to truncating when idle, so my current solution looks like:

(defun my/truncate-eshell-buffers ()
  "Truncates all eshell buffers"
  (interactive)
  (save-current-buffer
    (dolist (buffer (buffer-list t))
      (set-buffer buffer)
      (when (eq major-mode 'eshell-mode)
        (eshell-truncate-buffer)))))

;; After being idle for 5 seconds, truncate all the eshell-buffers if
;; needed. If this needs to be canceled, you can run `(cancel-timer
;; my/eshell-truncate-timer)'
(setq my/eshell-truncate-timer
      (run-with-idle-timer 5 t #'my/truncate-eshell-buffers))

After this, I no longer need to add 'eshell-truncate-buffer to 'eshell-output-filter-functions. It is working great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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