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
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
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
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
clean-buffer-list every n minutes or so, you could use
run-at-time, left as an exercise to the reader.
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
M-x kill-some-buffers. (Thanks Susan!). Or you can use