原文:Switch buffers in the next window.
next-buffer, previous-buffer 是內置的,默認綁定在 C-x <Left> ,C-x <Right>,作者增加了在其他 window 切換,以及開啟了 repeat-mode 方便多次切換。
相關代碼:
(defun my/next-buffer (&optional arg)
"Switch to the next ARGth buffer.
With a universal prefix arg, run in the next window."
(interactive "P")
(if-let (((equal arg '(4)))
(win (other-window-for-scrolling)))
(with-selected-window win
(next-buffer)
(setq prefix-arg current-prefix-arg))
(next-buffer arg)))
(defun my/previous-buffer (&optional arg)
"Switch to the previous ARGth buffer.
With a universal prefix arg, run in the next window."
(interactive "P")
(if-let (((equal arg '(4)))
(win (other-window-for-scrolling)))
(with-selected-window win
(previous-buffer)
(setq prefix-arg current-prefix-arg))
(previous-buffer arg)))
;; switch-to-buffer, but possibly in the next window
(defun my/switch-buffer (&optional arg)
(interactive "P")
(run-at-time
0 nil
(lambda (&optional arg)
(if-let (((equal arg '(4)))
(win (other-window-for-scrolling)))
(with-selected-window win
(switch-to-buffer
(read-buffer-to-switch
(format "Switch to buffer (%S)" win))))
(call-interactively #'switch-to-buffer)))
arg))
(defvar-keymap buffer-cycle-map
:doc "Keymap for cycling through buffers, intended for `repeat-mode'."
:repeat t
"n" 'my/next-buffer
"p" 'my/previous-buffer
"b" 'my/switch-buffer)
將 my/next-buffer ,my/previous-buffer 綁定到方便的快捷鍵,例如 C-x C-n、C-x C-p 。
需要切換時:C-x C-n 喚起 my/next-buffer, 由於開啟了 repeat-mode,之後可以用 n,p,b 快速切換 buffer。
對於切換常用 buffer 挺方便的。
