2

I have a lot of buffers in *Buffer List*.

    a.hs      1708 Haskell          ~/src/my-project/src/a.hs
    b.hs       581 Haskell          ~/src/my-project/src/b.hs
    c.hs      1711 Haskell          ~/src/other-project/tests/c.hs
    d.hs       687 Haskell          ~/src/random-project/d.hs

Is there a convenient way to mark rows (maybe similarly to Dired) using patterns or regex? The intention is to mark/kill all buffers that refer to files in my-project.

2
  • 3
    Have you tried M-x ibuffer? Quote the doc: ( ‘% f’ - Mark buffers by their filename, using a regexp. )
    – TerryTsao
    Commented Mar 3, 2022 at 5:24
  • 3
    +1 for ibuffer. I have rebound C-x C-b to ibuffer and have been using it exclusively for some years now. In the ibuffer buffer, you can do ? for help.
    – NickD
    Commented Mar 3, 2022 at 14:23

1 Answer 1

4

I join others in recommending Ibuffer, which is similar to the classic buffer list (Buffer Menu mode) but has a lot more features. (That's if you're not using something even fancier like Ido or Helm.) M-x ibuffer opens the buffer list in the current window, like M-x buffer-menu. M-x ibuffer-other-window opens the buffer list in another window (like the default C-x C-b) and (unlike C-x C-b) selects it.

Ibuffer has many commands that are similar to Dired. You can use d to flag the current item for deletion and x to actually delete all items flagged for deletion. You can use m to put a mark on the current item and D to delete the items that are marked (with a confirmation prompt). There are also features to mark buffers based on their properties, but they're slightly different from Dired's. In Ibuffer, use % f to mark buffers by their file name (that's the full path including directories). So you can delete my-project buffers with

% f ~/src/my-project/ RET D y

Another possible approach which works both in the classic C-x C-b Buffer Menu and in Ibuffer is to sort the buffer list by names. You can then quickly flag consecutive items for deletion. In the classic buffer list, use S (M-x tabulated-list-sort) while the cursor is at the beginning of a file name (the lines are sorted based on what's to the right of the cursor). In IBuffer, use s f to sort by file name (and s v to go back to the default sort by recency).

1
  • This is a very thorough answer, thank you.
    – dmvianna
    Commented Mar 4, 2022 at 18:35

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.