Personal website Rudolf Adamkovič

Home / Emacs Packages / Vertico


Buffer ordering

List buffers in the most recently used (MRU) order, matching

  • previous-buffer (C-x <left>)
  • next-buffer (C-x <right>)
(with-eval-after-load 'vertico
  (defun sort-previous-buffers+ (names)
    "Sort buffer NAMES matching the 'previous-buffer' order."
    (require 'seq)
    (let ((mru (seq-mapcat (lambda (window)
                             (seq-map (lambda (buffer-info)
                                        (buffer-name (car buffer-info)))
                                      (window-prev-buffers window)))
                           (window-list-1 nil nil t))))
      (seq-sort (lambda
                  (lhs* rhs*)
                  (let ((lhs (seq-position mru lhs*))
                        (rhs (seq-position mru rhs*)))
                    (cond
                     ((and lhs rhs) (< lhs rhs))
                     (lhs t)
                     (rhs nil)
                     (t (let ((lhs (seq-position names lhs*))
                              (rhs (seq-position names rhs*)))
                          (> lhs rhs))))))
                names))))
(with-eval-after-load 'vertico-multiform
  (add-to-list 'vertico-multiform-categories
               '(buffer (vertico-sort-function . sort-previous-buffers+))))

© 2025 Rudolf Adamkovič under GNU General Public License version 3.
Made with Emacs and secret alien technologies of yesteryear.