tempbuf-mode is a minor mode that enables buffers to get automatically deleted in the background when it can be deduced that they are no longer of any use.

It could be common for example to apply this mode to DiredMode buffers or read-only buffers visiting files, relieving you from having to delete each of them manually when the buffer list grows too large.

This package is not part of GNU Emacs, and is currently hosted by the EmacsWiki.

To enable this mode on dired buffers, put this line in your .emacs: (add-hook ‘dired-mode-hook ‘turn-on-tempbuf-mode)

I find it also very convenient to enable this mode in emacs customization buffers, W3 (Emacs’ Web Browser) buffer, UNIX ‘man’ documentation buffers, and any buffer with view-mode activated.

  (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)

If you want to enable this mode across “most” buffers you can take advantage of “after-change-major-mode-hook” which is the “normal hook run at the very end of major mode functions”. – mashdot

   (add-hook 'after-change-major-mode-hook 'turn-on-tempbuf-mode)

But for a much smarter implementation of this see: https://github.com/DarwinAwardWinner/dotemacs/blob/master/site-lisp/settings/tempbuf-settings.el

Do you know midnight.el in Emacs distribution? It provides clean-buffer-list function. – rubikitch

Here’s a patch that will enable users of this mode to provide an alternate function for notifying of a closing buffer. I use Growl, for example. YMMV, but it works for me so far.

diff -r 2e783f454a3b .emacs.d/vendor/tempbuf.el
--- a/.emacs.d/vendor/tempbuf.el	Mon Mar 08 14:42:34 2010 -0700
+++ b/.emacs.d/vendor/tempbuf.el	Mon Mar 08 14:45:35 2010 -0700
@@ -128,6 +128,11 @@
   :group 'tempbuf :type '(choice (const :tag "No message." nil)
+(defcustom tempbuf-kill-message-function nil
+  "Function used to deliver the message that a buffer was killed.
+If nil, use the default emacs messaging facility"
+  :group 'tempbuf :type 'function)
 (defcustom tempbuf-mode-hook nil
   "Hook run after tempbuf mode is activated in a buffer."
   :group 'tempbuf :type 'hook)
@@ -265,8 +270,7 @@
 	    (kill-buffer buffer))
 	  (when tempbuf-kill-message
 	    (unless (buffer-live-p buffer)
-	      (message tempbuf-kill-message
-		       name))))))))
+          (funcall (or tempbuf-kill-message-function 'message) (format tempbuf-kill-message name)))))))))
 (defun tempbuf-post-command ()
   "Update `tempbuf-last-time'."

I have included the patch in the latest tempbuf.el so you can now use M-x customize-variable tempbuf-kill-message-function to choose your favorite message notification function, i.e. growl, from Lisp:growl.el --MicheleBini