Split buffers in groups
It is possible with tabbar. You can add rules to group buffers in groups. Here's a basic snippet:
(defun tabbar-buffer-groups ()
"Returns the list of group names the current buffer belongs to."
(list
(cond
;; ADD RULES TO SPLIT BUFFERS IN GROUPS HERE!
;; if buffer is not grouped by the rules you would add above
;; put it in the "General" group:
(t
"General"
))))
Example rules:
- List buffer names:
((member (buffer-name)
'("*scratch*" "*Messages*" "*Help*"))
"Common" ;; this is a group name
)
- Regarding common buffers I prefer to put in "Common" each buffer which name starts with a star. This gives an example of making a buffer for this rule:
((string-equal "*" (substring (buffer-name) 0 1))
"Common"
)
- Here's an example of grouping buffers by major-mode:
((memq major-mode
'(org-mode text-mode rst-mode))
"Text"
)
- Here's an example of grouping buffers based on mode they are derived from:
((or (get-buffer-process (current-buffer))
;; Check if the major mode derives from `comint-mode' or
;; `compilation-mode'.
(tabbar-buffer-mode-derived-p
major-mode '(comint-mode compilation-mode)))
"Process"
)
- Here's an example of grouping tabs by regexp:
((string-match "^__" (buffer-name))
"Templates"
)
- Group buffers by major mode:
(if (and (stringp mode-name)
;; Take care of preserving the match-data because this
;; function is called when updating the header line.
(save-match-data (string-match "[^ ]" mode-name)))
mode-name
(symbol-name major-mode))
Once you composed the rules, you can press on + or - on tabbar's tab line to toggle groups, and also ◀ and ▶ to switch between buffers. Or just bind the following defuns:
tabbar-forward
tabbar-backward
tabbar-forward-group
tabbar-backward-group
and move between tabs and tab groups with keyboard.
Personally I group tabs, so that I see what is open, but navigate them with ido-switch-buffer
.
Switch between set of rules
Also one can define different set of buffer grouping rules and cycle between these. Here's an example of cycling between two set of buffer grouping rules:
;; tab-groups!
(setq tbbr-md "common")
(defun toggle-tabbar-mode ()
"Toggles tabbar modes - all buffers vs. defined in the `tabbar-buffer-groups'."
(interactive)
(if (string= tbbr-md "groups")
(progn ;; then
(setq tabbar-buffer-groups-function 'tabbar-buffer-groups-common)
(setq tbbr-md "common"))
(progn ;; else
(setq tabbar-buffer-groups-function 'tabbar-buffer-groups)
(setq tbbr-md "groups"))))
;; by default - all tabs:
(setq tabbar-buffer-groups-function 'tabbar-buffer-groups-common)
This toggles between tabbar-buffer-groups-common
and tabbar-buffer-groups
tab grouping defuns.
Sort tabbar buffers by name
I find it beneficial to sort tabbar buffers by name. Here's how to get it:
(defun tabbar-add-tab (tabset object &optional append_ignored)
"Add to TABSET a tab with value OBJECT if there isn't one there yet.
If the tab is added, it is added at the beginning of the tab list,
unless the optional argument APPEND is non-nil, in which case it is
added at the end."
(let ((tabs (tabbar-tabs tabset)))
(if (tabbar-get-tab object tabset)
tabs
(let ((tab (tabbar-make-tab object tabset)))
(tabbar-set-template tabset nil)
(set tabset (sort (cons tab tabs)
(lambda (a b) (string< (buffer-name (car a)) (buffer-name (car b))))))))))