12
  1. M-x ibuffer
  2. Select a buffer
  3. Kill that buffer

After the buffer is killed, the ibuffer buffer re-appears, but it still shows the buffer name killed in step 3.

Is it possible to have ibuffer auto-refresh the list of buffers?

Kevin Wright
  • 359
  • 1
  • 16
  • If you press Ctrl-x Ctrl-b again in ibuffer, it makes autorefresh. – ofenerci Aug 30 '17 at 07:59
  • @ofenerci For me (v25.2.2) C-x C-b opens an additional window \*Buffer List\* with the current list of buffers, but the \*Ibuffer\* is left unchanged – user2740 Jan 25 '19 at 14:08

2 Answers2

14

Running the command ibuffer-auto-mode in an Ibuffer buffer makes it refresh the display after each interactive command.

There doesn't appear to be a direct way of activating it automatically. You can put this in your init file:

(add-hook 'ibuffer-mode-hook (lambda () (ibuffer-auto-mode 1)))
  • 2
    This accepted solution breaks the selection in other buffer. When I enable that hook, if I do a selection on any buffer, then invoke `ibuffer`, the selection is lost. – nephewtom Jun 23 '17 at 18:10
  • `(setq ibuffer-mode-hook '(ibuffer-auto-mode))` should also do the trick if there's nothing else being done. – Amory Mar 30 '21 at 16:37
  • Word of caution: I noticed that ```ibuffer-auto-mode``` shows up a lot on my profiler (windows) so I ended up disabling it. – Marco Craveiro Apr 22 '21 at 16:45
9

The right way to do it is to introduce support in ibuffer for auto-revert-mode. This can be achieved by defining buffer-stale-function for those those buffers. Arguably, since buffer-menu supports that feature, it'd be good to have it upstream for ibuffer too, but that doesn't seem to be the case for now.

Anyway, here's a way to do it:

(defun my-ibuffer-stale-p (&optional noconfirm)
  ;; let's reuse the variable that's used for 'ibuffer-auto-mode
  (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed))

(defun my-ibuffer-auto-revert-setup ()
  (set (make-local-variable 'buffer-stale-function)
       'my-ibuffer-stale-p)
  (set (make-local-variable 'auto-revert-verbose) nil)
  (auto-revert-mode 1))

(add-hook 'ibuffer-mode-hook 'my-ibuffer-auto-revert-setup)

Note: in general, one would need to define a value for revert-buffer-function, but ibuffer already does that (it's set to ibuffer-update)

Sigma
  • 4,510
  • 21
  • 27
  • 1
    Is there a reason to prefer this method over `ibuffer-auto-mode`? – nispio Oct 15 '14 at 02:35
  • 1
    Well `auto-revert-mode` uses timers, and `ibuffer-auto-mode` uses `post-command-hook`, so there's an obvious *difference*; but offhand I couldn't say if one was preferable to the other. – phils Oct 15 '14 at 02:45
  • If you're running Emacs 24.3+, you could use the less-cumbersome `(setq-local buffer-stale-function ...)` in place of `(set (make-local-variable 'buffer-stale-function) ...)` – phils Oct 15 '14 at 02:57
  • 4
    @nispio I guess I tend to prefer generic solutions to ad-hoc ones. So in that sense, making `ibuffer` buffers behave like others when it comes to refreshing outdated content feels cleaner to me. Also, I generally dislike abusing `post-command-hook` that way: it's really overkill when you happen to do something completely unrelated in the buffer. Finally, if some kind of keystroke is required for content to be refreshed, it might as well be `g`, which is the explicit way of updating content. But granted, it's probably a philosophical issue :) – Sigma Oct 15 '14 at 02:57
  • 1
    When I use this method, I keep getting a message ``Reverting buffer `*Ibuffer*'.`` The message is intrusive because it keeps interrupting me when I am working in the minibuffer. The message appears whether the `ibuffer` buffer is visible or not. – nispio Oct 15 '14 at 03:31
  • 1
    @nispio `(setq auto-revert-verbose nil)`, potentially locally – Sigma Oct 15 '14 at 03:34
  • I am trying this solution, since the accepted one breaks the selection in other buffer. But this one shows 4 buffers in *Ibuffer* intermittently. Its buffer names shown are: *Minibuf-0*, *code-conversi...*, *Echo Area 0*, *Echo Area 1*. – nephewtom Jun 23 '17 at 18:14
  • @nephewtom you should probably add `" .*"` to `ibuffer-never-show-predicates` – Sigma Jun 25 '17 at 05:44
  • Thanks Sigma. @nispio, now you have a reason to prefer this method. – nephewtom Jul 13 '17 at 15:04
  • Another reason for prefering this method is that the ibuffer's point will be restored every time you switch the cursor to a different buffer and then back to ibuffer. – mikl Feb 12 '20 at 17:28
  • I logged in just to upvote this! The use of `post-command-hook` for this is definitely an abuse, and was breaking TRAMP for me. I can add that I was also getting hidden buffers shown, and the reason for this, as far as I can tell, is that Ibuffer does set `ibuffer-update` to `revert-buffer-function`, but its arguments are different from those `revert-buffer-function` expects. So I defined `(defun my/ibuffer-update (&optional _ignore-auto _noconfirm) (ibuffer-update nil t))` and set that to `revert-buffer-function`. – gusbrs Jan 21 '21 at 18:57