Search code examples
emacsido

emacs ido-buffer : group buffers by directories


Is it possible to view the open buffers grouped by directories in emacs ido-buffer mode, in some sort of tree representation?


Solution

  • "emacs ido-buffer mode" ??

    Did you mean ibuffer? If so...

    It's not grouping1, but sorting by filename is a fairly useful approximation, and is available by default.

    sf

    Unfortunately (to my mind) dired buffers aren't included. You might fix that by defining a variant of the sorter which includes them, and then remapping the binding:

    (eval-after-load 'ibuffer
      '(progn
         (define-ibuffer-sorter filename/directory/process
           "Sort the buffers by their file name/process name."
           (:description "file name")
           (string-lessp
            (or (buffer-file-name (car a))
                (let ((dir (buffer-local-value 'dired-directory (car a))))
                  (if (consp dir) (car dir) dir))
                (let ((pr-a (get-buffer-process (car a))))
                  (and (processp pr-a) (process-name pr-a))))
            (or (buffer-file-name (car b))
                (let ((dir (buffer-local-value 'dired-directory (car b))))
                  (if (consp dir) (car dir) dir))
                (let ((pr-b (get-buffer-process (car b))))
                  (and (processp pr-b) (process-name pr-b))))))
    
         (define-key ibuffer-mode-map
           [remap ibuffer-do-sort-by-filename/process]
           'ibuffer-do-sort-by-filename/directory/process)))
    

    1 A function to dynamically group by directory would be nifty.