如何在Emacs Ibuffer中实现类似Buffer-menu的T键过滤文件访问缓冲区功能?
Hey there! I’ve got you covered for setting up that handy T key functionality in Ibuffer to only show file-backed buffers, just like Buffer-menu does. Here’s a straightforward way to make this work:
Step 1: Define a toggle function
First, we’ll create a custom function that switches between showing only file buffers and showing all buffers. Add this to your Emacs config (init.el or similar):
(defun my-ibuffer-show-only-file-buffers () "Toggle between showing only file-backed buffers and all buffers in Ibuffer." (interactive) (setq ibuffer-filtering-alist (if (equal ibuffer-filtering-alist '((mode . file-mode))) nil ; Clear filter to display all buffers '((mode . file-mode)))) ; Apply filter for file-only buffers (ibuffer-update nil t)) ; Refresh the Ibuffer view
This function checks if we’re already filtering for file-mode buffers. If yes, it clears the filter to show everything; if not, it applies the filter to only display buffers connected to actual files.
Step 2: Bind to the T key
Next, we’ll bind this function to T (Shift + t) specifically in Ibuffer mode, so it behaves exactly like Buffer-menu. Add this right after the function above:
(add-hook 'ibuffer-mode-hook (lambda () (define-key ibuffer-mode-map (kbd "T") 'my-ibuffer-show-only-file-buffers)))
How to use it
Open Ibuffer with M-x ibuffer, then hit T—you’ll see only file-backed buffers. Hit T again, and all buffers (like scratch, Messages, etc.) will reappear.
If you ever want to manually apply or clear filters without the toggle, you can use built-in commands like M-x ibuffer-filter-by-file-buffer to show only file buffers, or M-x ibuffer-clear-filter to go back to showing everything.
内容的提问来源于stack exchange,提问作者Martin

