One reason, why I blog about emacs is that I’m often learning new things from people who leave a comment on my blog. My latest cool toy, thanks to a mention from foo is ibuffer. I had heard of it already (and had a vague idea of what it does), but had not investigated it any depth.
So, what does ibuffer do and why is it cool? It is a powerful substitute for list-buffers
which by default is bound to C-x C-b
. You can get started by typing M-x ibuffer <RETURN> h
(for help).
The main features provided are:
- sorting (with a
s
prefix) - filtering (with a
/
prefix) - marking (with
*
or%
prefixes) and - processing (which covers a multitude of sins)
I like to sort the buffers lexicographically before I begin with s a
. If it turns out I always do this, I’ll customize ibuffer-default-sorting-mode
.
I’ve come up with a few simple recipes so far:
* s
(mark star buffers)D
(delete all marked buffers)* r k
(remove read-only buffers from ibuffer)% n
(mark buffers by regex)U
(regex replace)
One thing that ibuffer doesn’t have is a function that compares two marked buffers. However, its easy enough to add one.
(defun ibuffer-ediff-marked-buffers () (interactive) (let* ((marked-buffers (ibuffer-get-marked-buffers)) (len (length marked-buffers))) (unless (= 2 len) (error (format "%s buffer%s been marked (needs to be 2)" len (if (= len 1) " has" "s have")))) (ediff-buffers (car marked-buffers) (cadr marked-buffers)))) (define-key ibuffer-mode-map "e" 'ibuffer-ediff-marked-buffers)
We might as well replace the default binding for C-x C-b
.
(global-set-key (kbd "C-x C-b") 'ibuffer)
My favorite buffer lister is bs-show: it doesn’t take whole screen, what I really like (it gives you ability to preview buffer), and it’s possible to divide buffer in groups (and you see only one group at the time by default).
I use ibuffer, too. What I like best is it’s ability to group buffers by mode/file/whatever. You can do that dynamically, but I use a static cofiguration instead, so that I have my favourite grouping whenever I start it.
(setq ibuffer-saved-filter-groups
‘((“default”
(“Chat” (or
(mode . rcirc-mode)))
(“Organization” (or
(mode . diary-mode)
(mode . org-mode)
(mode . org-agenda-mode)))
(“Gnus & News” (or
(mode . message-mode)
(mode . bbdb-mode)
(mode . mail-mode)
(mode . gnus-group-mode)
(mode . gnus-summary-mode)
(mode . gnus-article-mode)
(name . “^\\.bbdb$”)
(name . “^\\.newsrc-dribble”)
(mode . newsticker-mode)))
(“Multimedia” (or
(mode . emms-playlist-mode)
(mode . emms-browser-mode)))
(“Files” (filename . “.*”))
(“File Management” (or
(mode . dired-mode)
(mode . shell-mode)))
(“Documentation” (or
(mode . Info-mode)
(mode . apropos-mode)
(mode . woman-mode)
(mode . help-mode)
(mode . Man-mode))))))
I recently started using this … I like ibuffer-set-filter-groups-by-mode
Filtering is the main reason I use IBuffer. How handy!
There are a number of other options for filtering — I’ve added them to the EmacsWiki page at: http://www.emacswiki.org/emacs/IbufferMode#toc5
in particular “name” for buffer-name, and “filename” for filtering by.. the.. er, I think you get my drift.
Since filename can work for any part of the path, if you filter on a project directory, anything you have open is now grouped.
@Alexander – did you try IBuffer. If so, apart from the auto-sizing, how does it compare to bs-show?
@Tassilo – nice tip! I haven’t got around to setting any groups up yet, but it looks like a great feature.
@Ed – yes, grouping by major mode is useful when doing similar processing on similar code eh? 🙂
and last but not least…
@OtherMichael – cool, I didn’t know that filtering by filename included the entire path. That is very useful.
> @Alexander – did you try IBuffer. If so, apart from the auto-sizing, how does it compare to bs-show?
Yes, I’ve used it before bs-show. I switched because of auto-sizing. Main difference for me was that I see only one type of buffer at a time (there is ‘a’ hotkey, which switch you to alternate view – mine shows all buffers). But usually I need to switch to similar buffer, so actually bs-show works better for me.
I don’t remember what else I liked/disliked, though filtering is nice thing (I use isearch in bs-show).
This is my config for bs: http://hg.piranha.org.ua/conf/file/tip/.emacs.d/load/bs_init.el
I wrote it when I played with Lisp and its macros, so it may be more complex than it needs to be, but anyway it’s funny. 😉
If it is mainly due to the auto-resizing, ibuffer has a command that emulates bs-show.
ibuffer-bs-show
Command: Emulate `bs-show’ from the bs.el package.
I’ve been having problems implementing predicates for my ibuffer filters. I want to filter down to just those buffers that have a certain buffer-local variable that is set to a certain value. The problem is whenever I set the predicate via (ibuffer-filter-by-predicate), it will evaluate it correctly the first time, but then the value of the predicate will be lost whenever I try to update (like when pressing ‘g’). I was wondering if anyone had implemented one of these predicate functions similar to this.
Thanks!
[…] A curious programmer, includes a small function to compare two marked buffers using ediff. […]
[…] An Overview | Mastering Emacs PyPy Status Blog: Efficiently Implementing Python Objects With Maps IBuffer « A Curious Programmer The Abstract Factory: ibuffer: If you do not use it, you are insane emacs-fu: dealing with many […]
Thanks for the tip to make IBuffer compare 2 marked buffers. That’s exactly what I was looking for when I arrived here.
It can be enhanced to make it compare 2 or 3 buffers, like this:
(defun ibuffer-ediff-marked-buffers ()
(interactive)
(let* ((marked-buffers (ibuffer-get-marked-buffers))
(len (length marked-buffers)))
(cond
((= 2 len)
(ediff-buffers (car marked-buffers) (cadr marked-buffers)))
((= 3 len)
(ediff-buffers3 (car marked-buffers) (cadr marked-buffers)
(caddr marked-buffers)))
(t
(error (format “%s buffer%s been marked (needs to be 2)”
len (if (= len 1) ” has” “s have”)))))))