2010-03-20

showing the buffer position in the mode-line

I do quite a bit of scrolling in emacs, but I hardly ever use the scroll bar for that. The main reason for still having the scroll bar is that it gives me some indication where I am in the buffer. Of course, there is some information in the mode-line, and you can get some more with size-indication-mode, but it's not as immediately obvious as the scroll bar.
But recently, I discovered Lennart Borgman's sml-modeline, which combines all of the scroll bar information into a nice visual indication on the modeline, and I have been happily using it, and got rid of my scroll bar.
Put you sml-modeline in your load-path, and the following fragment in your .emacs should do the trick:
(if (require 'sml-modeline nil 'noerror)    ;; use sml-modeline if available
  (progn 
    (sml-modeline-mode 1)                   ;; show buffer pos in the mode line
    (scroll-bar-mode -1))                   ;; turn off the scrollbar
  (scroll-bar-mode 1)                       ;; otherwise, show a scrollbar...
  (set-scroll-bar-mode 'right))             ;; ... on the right

Note, there is a older version available in Emacswiki which has some problems (such as conflicting with the Standard ML editing mode for emacs); thus, for now it's better to us the Launchpad version; the instructions above apply to that version.

2010-03-08

cleaning up buffers automatically

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.