0

I'm trying to group my buffers by tramp connections (like ibuffer-tramp does) but inside the tramp filters it keep my custom filter groups that I defined via

(setq ibuffer-saved-filter-groups
 '(("Default"
    ("Unsaved" (modified))                      ; All unsaved buffers
    ("Stars"   (starred-name))                  ; Group *starred*
    ("Start"   (name          . "\\*scratch\\*")) ; By regexp
    ("Dired"   (mode          . dired-mode))    ; Filter by mode
    ("Org"     (filename      . ".org"))        ; By filename
    ("Scheme"  (directory     . "~/scheme*"))   ; By directory
    ("Gnus" (or                                 ; Or multiple!
           (saved        . "gnus")
           (derived-mode . bbdb-mode))))))

My current issue is that ibuffer-tramp overwrites by ibuffer-saved-filter-groups. Sadly i'm super new to lisp and I'm not finding a way to put the two together. In the ibuffer-tramp documentation it says :

;; Alternatively, use `ibuffer-tramp-generate-filter-groups-by-tramp-connection'
;; to programmatically obtain a list of filter groups that you can
;; combine with your own custom groups.

I was hoping this can help me manage to create subgroups or similar but I couldn't find a solution.

At the very least it could be nice if all tramp similar connections (same hostname) are grouped together in separate filters. Like I would get the [ host1 ] at the top of ibuffer list then [ host 2 ] and then [ Unsaved ] etc...

How can I do that?

1 Answer 1

0

I ended up using perspective with iBuffer. By default using persp-iBuffer only show current perspective buffers. But when iBuffer is updated, all buffers are displayed (from ALL persp) thus I added this advice:

;; Function to add advice to `ibuffer-update` for filtering buffers
(defun persp-ibuffer-advise-update (&rest _args)
  "Advise `ibuffer-update` to filter buffers before updating using the `persp-ibuffer` logic."
  (when (and persp-mode (not (eq this-command 'ibuffer)))
    (setq ibuffer-maybe-show-predicates (append ibuffer-maybe-show-predicates
                                               (list #'(lambda (buf) (persp-buffer-filter buf t)))
                                               ido-ignore-buffers))))

;; Add the advice to `ibuffer-update` to apply the filtering logic
(advice-add 'ibuffer-update :before #'persp-ibuffer-advise-update)

Now instead of opening a new tramp connection directly via C-x d is use a custom function that creates a new persp and opens the tramp connection:

;;; Custom function to connect to an SSH and create a persp at the same time
(defun my-validate-ssh-target (ssh-target)
  "Validate the SSH target format (user@host)."
  (if (string-match-p "^[^@]+@[^@]+$" ssh-target)
      ssh-target
    (error "Invalid SSH target format. Please use 'user@host'.")))

(defun my-persp-ssh ()
  "Prompt for an SSH target and create a new perspective with a dired buffer for that connection."
  (interactive)
  ;; Prompt for SSH target
  (let* ((ssh-target (my-validate-ssh-target (read-string "SSH Target (user@host): ")))
         (persp-name (read-string "New perspective name: ")))
    ;; Ensure persp-mode is enabled
    (unless persp-mode
      (persp-mode 1))
    ;; Create new perspective
    (persp-switch persp-name)
    ;; Open TRAMP dired buffer in new perspective
    (dired (concat "/ssh:" ssh-target ":"))))

This is a working solution to my problem, but I agree that it doesn't directly use subgrouping. (I'm not sure I can thus close the question)

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.