I'd like to find a way to cycle through all buffers that use the same major mode as the current buffer. For all major modes, including the special buffers.

Use case example: when refactoring code I often use several instances of the super useful deadgrep buffers. I search for things before I start to identify the locations that must be edited, and regularly update them as I go up to the point where nothing is found. So I'd like to quickly iterate through all of those buffers in a window, while I use other windows to hold code buffers (where I also might want to cycle through them).

How can I do this?

Investigation so far: I tried the iflipb external package and the built-in Buffer Selection bs.el. So far I have not found a way to use them to do what I want, but I'm going through to code to see if I can do that.

  • FYI I did find a similar question here. Also it seems that this script allows you to set a 'buffer-switch filter' May 30, 2021 at 21:20
  • @dalanicolai Thanks. I'd like to see if there is something that already exists in some buffer management library, ideally built-in Emacs though. I would have thought that it would already part of some existing library.
    – PRouleau
    May 30, 2021 at 21:45
  • I did not find libraries that do that specifically. Drew has written Icicle that provides ability to do it. Emacs built-in bs.el provides something that can be extended to do that but it requires opening bs buffer with bs-show and then selecting the specialized configuration. In PEL, I added support for bs for creating same-mode configurations and also to quickly navigate through buffers of same major mode. I also added commands that do that without having to use bs. That is github.com/pierre-rouleau/pel/blob/master/pel-buffer.el
    – PRouleau
    Jun 1, 2021 at 15:59

1 Answer 1


If you use Icicles then just use one of these with C-x b:

  • C-0 (or M-0 or C-u 0 etc.): The candidate buffers will have the same mode as the current buffer
  • C-u: Same, but include also buffers whose mode is derived from that of the current buffer.

In Icicle mode, C-x b is bound to multi-command icicle-buffer.

And you need not choose such filtering before you invoke the command. You can instead filter the same way on the fly, during buffer-name completion:

  • C-x M +: Keep only buffers whose mode is the same as the current buffer.

  • C-x C-m +: Same, except keep also buffers whose mode is derived from that of the current buffer.

The same is true for all Icicles commands that want a buffer name as input.

So for example, you can use C-0 with C-x k to have as candidate buffers to kill, only those with the same mode as the current buffer.

And because that too is a multi-command, you can use it to kill multiple buffers with a single invocation. You can even kill all candidates whose name matches your current minibuffer input, using C-!.

See Icicles: Buffer-Name Input.

  • Thanks. Does this command cycles through buffer names or buffer themselves? What I'd like to have is a command that visit the next or previous buffer of the same mode in the current window.
    – PRouleau
    May 31, 2021 at 3:10
  • C-x b visits a buffer in the selected window, just like it does in vanilla Emacs. You speak of "next"/"previous" buffer. Yes, you can cycle among buffers with C-down etc., and cycling is in the current sort order. (And you can change the sort order on the fly, using C-, - there are several sorts, i.e., meanings of "next"/"previous".) But in addition to Icicles there are likely other libraries, as well, that let you do what you ask (same mode etc.), and no doubt some are smaller and only about buffer switching. Others will provide other answers to your question.
    – Drew
    May 31, 2021 at 3:41
  • Thanks Drew. I ought to try Icicles.
    – PRouleau
    May 31, 2021 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.