, , , , Posted on 1 April 2025 by

Weekly Emacs tip #16: Managing your buffers more easily with ibuffer

Working in Emacs often means you end up with loads of buffers: one for every file you opened, but also others for e.g. Magit, Tramp connections or flycheck. This is fine, in principle. Emacs doesn’t really care.

However, as the number of buffers grows, you may find that switching between buffers becomes difficult as you have to wade through the list of buffers. Completion helps, but still, a bit of help would be nice. For example, I generally close the buffers related to a project when I know I won’t be working on it for some time. Of course, you can close buffers one by one using C-x k (the kill-buffer command), but that can become tedious for larger projects.

This is where the built-in ibuffer package comes to the rescue. The command ibuffer opens a buffer listing all buffers, similar to list-buffers (bound to C-x C-b by default). The difference, however, is that the ibuffer is “actionable”: you can filter the list in various ways (e.g. by filename extension), mark buffers for certain actions (like killing them), etc. The View and Mark menus will help you discover those options. For example, moving to a certain buffer and pressing Enter (RET) will replace the ibuffer buffer with the buffer you selected.

As an example of how I manage buffers, the screenshot below shows three file-backed buffers: two LaTeX files and my .emacs file (the other buffers, the ones with the asterisks, were created automatically be Emacs). To filter on the LaTeX files, press / ., which filters for file extension. Enter tex to show only the LaTeX files (see the second screenshot). Notice how the top line shows the currently filters. And, yes, you can have multiple filters applied at the same time, for example, both a file extensions filter and a directory filter to only show those files in the selected directory.

In the third screenshot I have used d to mark both LaTeX buffers for deletion. This doesn’t mean the underlying files get deleted. It just means that those two buffers will be killed. Next, pressing x (for “execute”) will ask if you really want to kill those two buffers. After that you can press / / to reset/remove the filters. Press q to quit and g to refresh the ibuffer buffer.

Instead of filtering by file extension we could have used / m to filter by mode or / n to filter by buffer name. Each of these accept a regular expression for even more targeted filtering. To learn more about the filter options, press / followed by C-h to show the filter help. Pression the ? in an ibuffer buffer will show the help for this minor mode with more details on the various actions, sorting commands (with s) and filter options.

This is how I configure the ibuffer package. I tell it to bind to C-x C-b (thus overwriting the default list-buffers), and I want ibuffer to open in another (Emacs) window, instead of “overwriting” the buffer I was in.

;; Set up the ibuffer for editing the buffer list
(use-package ibuffer
  :bind
  ;; Use C-x C-b to show ibuffer (from where I can e.g. kill a series
  ;; of buffers) instead of just showing the (read-only) buffer list
  ("C-x C-b" . ibuffer)
  :custom
  ;; Open ibuffer in another window instead of overwriting the one I
  ;; am working in.
  (ibuffer-use-other-window t)
  )

Some links with more information:

No Comments

Let us keep you updated!

Once a month we’ll send you an overview of our newest articles. No spam, we promise.

Thank you for signing up! We've send you an confirmation mail.