Ibuffer

The beauty of Emacs workflow is the change in mindset from the common text editors around. You have no visible tabs available to switch between them, or better yet, you do not need them.

When I came from vim to learn Emacs, I remember I was a huge user of Neotree plugin and that was a must have in my .vimrc. However, the workflow I came to get used to in Emacs is so different that I never used any tools like that.

So, how do we manage several "buffers" opened at once? Well, we have Ibuffer.

Ibuffer, written by Colin Walters, is an advanced replacement for an Emacs package called BufferMenu . The package create a mode similar to `Dired` where you can modify and poke with buffers instead of directories.

An usual binding that you find in a lot of emacs.d setup across the web is to map C-x C-b to Ibuffer. However I only managed to start using Ibuffer a few months ago because my workflow is very intensive of ido-switch-buffer which prevents Ibuffer from being called more often.

If you want to keep exploring the Ibuffer mode, there is no place like Emacs self documentation. So, simply press h and start interacting with the options. I also recommend the Emacs Wiki entry.

Let's take a look.

Sorting your buffers

There are two ways to change the default settings to customize how Ibuffer mode sorts all your buffers; i) using a default sorting function or ii) written your customized groups.

Case 1: Sorting functions

(setq ibuffer-default-sorting-mode 'major-mode)

This option is very simplistic and offers very low customization to the eager Emacs user.

Case 2: Customized buffers group

Here we have a lot more control about how the buffers are going to be displayed.

(setq ibuffer-saved-filter-groups
      '(("default"
         ("EMACS" (or (filename . ".emacs.d")
                             (filename . "init.el")))
         ("ORG" (or (mode . org-mode)
                    (filename . "notes")
                    (filename . "meetings")))
         ("CLOJURE" (or (mode . clojure-mode)
                        (filename . "boot")
                        (filename . "\*.cljs")))
         ("PYTHON" (or (mode . python-mode)
                       (filename . "requirements.txt"))))))

;;; load the `default' filter group automatically when ibuffer start
(add-hook 'ibuffer-mode-hook
          '(lambda ()
             (ibuffer-switch-to-saved-filter-groups "default")))

After evaluating the previous settings, you should see something like this:

You can move between groups with TAB key.

Mark and perform operations

Any operation on Ibuffer mode is performed over selected buffers. The best way to find out more valid options inside the buffer is by pressing h and take a look at the documentation we have for the package.

The main operators that I am using right now:

Command Description
/ f to filter by filename
/ c to filter by content
/ RET to filter by mode
// remove the filter
% n mark buffers by their name, using a regexp
% m mark buffers by their major mode, using a regexp
% f mark buffers by their filename, using a regexp
% g mark buffers by their content, using a regexp
M-s a C-s do incremental search inside the marked buffers
Q do query replace in all selected buffers
O list lines in all marked buffers which match a regexp
RET view the buffer on this line
o as RET, but in another window
C-o as RET with o, but don't select the window

There are nice workflows you can check now. I'm trying to lose my mechanical memory of pressing C-x C-s every time that I change anything inside a buffer and that's for two reasons: i) I'm tired of useless key pressings; ii) I can't see diffs from buffer state and file state.

If you can see the diff between all the modified buffers and their corresponding files in the "original" versions, you can simply:

  • Press % m to mark all the modified buffers
  • Press = to see the diff between them and their files counterparts.

And many others, just take a look at h for help.

Conclusion

It's amazing the amount of hidden features Emacs have. I am spending a lot of time to be very minimalist with my configuration setup. I believe there is really much we can find wiht pretty vanilla Emacs setup or few packages. Today I manage to install only 17 packages which most of them are to increase support to missing modes such as PHP, Markdown and Clojure.

However, it's fine if you don't want to follow this path and wants to leverage all the power that Emacs's developers have put effort to create along the years. I personally recommend the following packages related to ibuffer.

Let's keep hacking!