Recently, I discussed some ways to deal with large numbers of buffers. Maybe we can also take a step back and ask why we have so many buffers in the first place - do we really need all of them?
Well, the obvious answer is: probably not. After a few days of
(emacs-uptime)
there are all kinds of temporary output buffers, man pages
and unchanged buffers you haven't touched in a long time. Let's get rid of
those!
midnight
For this very purpose, emacs provides midnight-mode
(as has done so for more
than a decade). At midnight, it looks at all the buffers, and determines which
of the buffers are no longer in use, and closes ('kills') them. Regardless of
its name, this cleanup does not necessarily have to take place at midnight,
but could be invoked at any time.
Setup is easy, just put the following in your .emacs
:
(require 'midnight)
Clearly, the package was designed for emacs instances that are running for
long times – for example, by default it clears buffers after having been
inactive for 3 days. I'm not sure if that use case is very common today.
Anyway, you can change it by setting clean-buffer-list-delay-general
(which
takes the number of days before a buffer becomes eligible for killing).
You can ask midnight-mode
to clean-up unused buffers right now with M-x clean-buffer-list
. Also, you can use some variables to control which buffers
are to be killed, and which ones specifically not:
clean-buffer-list-kill-buffer-names clean-buffer-list-kill-never-buffer-names clean-buffer-list-kill-regexps clean-buffer-list-kill-never-regexps
To run clean-buffer-list
every n minutes or so, you could use
run-at-time
, left as an exercise to the reader.
tempbuf
Another way to accomplish roughly the same is TempbufMode.
It seems a bit better equipped for shorter cleanup interval, and you have some killed. However, that requires you to add it to the modes where you'd like to more influence on the algorithm it uses to decide whether a buffer may be use it, something like:
;; download tempbuf: http://www.emacswiki.org/emacs/tempbuf.el (when (require 'tempbuf nil 'noerror) (add-hook 'custom-mode-hook 'turn-on-tempbuf-mode) (add-hook 'w3-mode-hook 'turn-on-tempbuf-mode) (add-hook 'Man-mode-hook 'turn-on-tempbuf-mode) (add-hook 'view-mode-hook 'turn-on-tempbuf-mode))
Side-note: I'm using the (when (require 'tempbuf nil 'noerror) ... )
pattern
here to make sure that my .emacs
also works when tempbuf
is not available.
Added: for cleaning-up your buffer list non-automatically, you can simply
use M-x kill-some-buffers
. (Thanks Susan!). Or you can use C-x C-b
.
4 comments:
I *never* start a new emacs instance. I have the same one up for days and days, so thanks for pointing out midnight mode! That solves a problem I've been wondering about for a while!
I agree with Searcher. In my mind I "just" rebooted so I did an emacs-uptime and saw that it was 5 days. So, yes, Midnight will be useful!
Great tip! I often end up with more than 500 buffers open, that I kill a few at a time in the buffer list. Thanks for pointing out midnight.
I concur, I have my Emacsen up for long periods of time (10 days and 28 days respectively (different VNCs)). I'd forgotten that I use midnight, but it definitely does help keep the clutter down.
Post a Comment