When you have many buffers opened, they are all shown in the buffer list.
Are they shown in the order of visit, from the latest to the remotest?
Can we organize the buffers in the buffer list somehow? For example resort them by their pathnames?
I assume you are referring to the list of buffers you get with the default key binding C-x C-b
, which calls list-buffers
. That command uses Buffer-menu-mode
which provides some key bindings for working with the list but has limited options for sorting or filtering. For more detail on what you can do in the list, try C-h f Buffer-menu-mode
.
Emacs includes a more powerful buffer list that you may prefer: ibuffer
. This mode provides a similar list of open buffers, but with additional capabilities for sorting, filtering, grouping, and otherwise working with the list. You can try it out with M-x ibuffer
, and if you like it you can add this to your Emacs init file to use ibuffer instead of the default list-buffers command:
(global-set-key (kbd "C-x C-b") 'ibuffer)
The easiest way to get started with ibuffer is to explore the menu. Look under 'View' for options to sort, filter, and mark buffers.
*Ibuffer*
).
Commented
Oct 5 at 1:26
Use C-h m
in the buffer-list buffer to get answers to these questions and more. You can click the header of a column to resort entries by that column. Click it again to reverse. To sort by pathname, click header File
.
If you don't have a mouse then move the cursor under the column you want to sort and use S
to sort it. Repeat to reverse the direction.
If you use C-h m
then near the beginning of the description of the mode you see See buffer-menu
for a description of its contents. Follow that link (using RET
, aka Enter) to see more info about the mode.
Near the end of that page of information you see this: See Buffer-menu-mode
for the keybindings available the Buffer Menu. If you follow that link then near the top of the next page of info (about Buffer-menu-mode
) you see this: Parent mode: tabulated-list-mode
. If you follow that link, to see info about the parent mode, there you will see additional keys that are available in Buffer-menu-mode
- keys that are not specific to it but are provided by its parent mode, tabulated-list-mode
.
Among those key descriptions you see this:
S tabulated-list-sort
That tells you that the key S
runs command tabulated-list-sort
. If you follow that link then you see this:
tabulated-list-sort is an interactive compiled Lisp function in
`tabulated-list.el'.
(tabulated-list-sort &optional N)
Sort Tabulated List entries by the column at point.
With a numeric prefix argument N, sort the Nth column.
It's a bit roundabout, but this tells you that if you put the cursor ( point is the cursor position) in a given column and you hit S
then you will sort the buffer according to that column.
The main lesson is that C-h m
gives you information about the current mode, whatever mode you are in. In that description, you can follow links to get more information.
With M-x ibuffer one can filter on pathname components. The commands I use are
/f filter using part of full pathname
// filter off
/g create named filter group and move group to top of ibuffer
C-k RET kill filter group (the one under the cursor)
C-y RET yank filter group
The above recipe does not completely answer the question, since it is not possible (AFAIK) to place the files/buffers of the latest filter group at the top of the ibuffer after killing the group.