Emacs之间切换缓冲区的标签页

4

有没有一种方法可以在不通过缓冲区列表或写出要切换到的缓冲区名称的情况下切换缓冲区?更具体地说,我想知道Emacs是否可以像Notepad ++中那样使用选项卡在缓冲区之间切换


2
这个问题也在superuser上有不同的回答:http://superuser.com/questions/208871/emacs-tab-between-buffers - jwernerny
1
我尝试了很多,最终停留在一个简单的iflip包上http://www.emacswiki.org/emacs-ja/iflipb。然而,随着时间的推移,你会发现几乎没有比"C-x b" + ido-mode更好的切换方式了。 - VitoshKa
4个回答

4
我从来没有经常使用 C-x <right> 或者 C-x <C-right>,因为如果我想要循环浏览多个缓冲区,它们会让我感到繁琐,所以我写了一些函数,让你可以继续使用 <C-right><C-left> 切换到上一个/下一个缓冲区,如果上一个命令也是一个 next/previous-buffer 命令的话。
例如,C-x <C-left> <C-left> <C-left> <C-right> <C-left> 会让你返回三个缓冲区,前进一个,然后再后退。
我假设 <C-left><C-right> 通常绑定到 forward/backward-word,并且作为后备选项显式地调用它们。
(defun my-forward-word-or-buffer-or-windows (&optional arg)
  "Enable <C-left> to call next-buffer if the last command was
next-buffer or previous-buffer, and winner-redo if the last
command was winner-undo or winner-redo."
  (interactive "p")
  (cond ((memq last-command (list 'next-buffer 'previous-buffer))
         (progn (next-buffer)
                (setq this-command 'next-buffer)))
        ((memq last-command (list 'winner-redo 'winner-undo))
         (progn (winner-redo)
                (setq this-command 'winner-redo)))
        (t ;else
         (progn (forward-word arg)
                (setq this-command 'forward-word)))))

(defun my-backward-word-or-buffer-or-windows (&optional arg)
  "Enable <C-left> to call previous-buffer if the last command
was next-buffer or previous-buffer, and winner-undo if the last
command was winner-undo or winner-redo."
  (interactive "p")
  (cond ((memq last-command (list 'next-buffer 'previous-buffer))
         (progn (previous-buffer)
                (setq this-command 'previous-buffer)))
        ((memq last-command (list 'winner-redo 'winner-undo))
         (progn (winner-undo)
                (setq this-command 'winner-undo)))
        (t ;else
         (progn (backward-word arg)
                (setq this-command 'backward-word)))))

(global-set-key (kbd "<C-left>") 'my-backward-word-or-buffer-or-windows)
(global-set-key (kbd "<C-right>") 'my-forward-word-or-buffer-or-windows)

4

Emacs 22.1及以上版本支持使用previous-bufferC-x <left arrow>)和next-bufferC-x <right arrow>)命令。

这两个命令可以通过此脚本添加到旧版Emacs中。


是的,请查看我的回答中第一个链接中在最近使用的两个缓冲区之间切换一节。 - Frédéric Hamidi

1

我自己使用 Icicles 进行缓冲区切换,但是...

如果你想重复上一个命令多次,只需使用 C-x z z z z z z... 例如,在这种情况下,你可以使用 C-x left C-x z z z...

如果这太麻烦了,像其他人建议的那样将 (next|previous)-buffer 绑定到其他可重复按键上。

但是可重复的按键非常受欢迎。如果你不想浪费任何按键,甚至可以将这些命令放在一个前缀键上,这样,例如,你可以做,例如,C-x left left left... 这里有一个从Bookmark+代码中提取出来的技巧:

(defun my-repeat-command (command) "重复执行 COMMAND." (let ((repeat-message-function 'ignore)) (setq last-repeatable-command command) (repeat nil)))
(defun my-next-whatever-repeat (arg) ; `C-x right' "跳到第 N 个下一个 whatever. N 默认为 1,表示下一个 whatever。 普通的 `C-u' 表示从第一个 whatever 开始(不重复)." (interactive "P") (require 'repeat) (my-repeat-command 'next-whatever))
(define-key ctl-x-map [right] 'my-next-whatever-repeat)

1
尽管以下建议使用了buffer-list、Ivy(或Helm)和evil,但我认为这是一种不错的、几乎等效的替代方法,可以在使用Spacemacs时更新缓冲区列表,而不必使用常见的Ctrl-TAB方式。当然,这些命令可以根据个人配置进行调整(纯Emacs)。
(evil-global-set-key 'motion (kbd "<C-tab>") 'ivy-switch-buffer)
(evil-global-set-key 'insert (kbd "<C-iso-lefttab>") 'ivy-switch-buffer)
(define-key ivy-mode-map (kbd "<C-tab>") 'ivy-next-line-and-call)
(define-key ivy-mode-map (kbd "<C-iso-lefttab>") 'ivy-previous-line-and-call)

或者对于Helm的等效物。
(evil-global-set-key 'motion (kbd "<C-tab>") 'helm-buffers-list)
(evil-global-set-key 'motion (kbd "<C-iso-lefttab>") 'helm-buffers-list)
(define-key helm-map (kbd "<C-tab>") 'helm-follow-action-forward)
(define-key helm-map (kbd "<C-iso-lefttab>") 'helm-follow-action-backward)

最后一个命令的键序列在我的键盘上表示为Ctrl-Shift-tab。您可以使用C-h k C-S-tab找到要使用的键序列。

实际上,在释放<C-tab>后,仍然需要按下RETC-l

对于使用Helm的Evil用户,一种可能更好的选择是将helm-buffers-list绑定到C-jC-k,然后将helm-follow-mode-persistent设置为t,并在helm-buffers-list缓冲区中使用C-c C-f激活helm-follow-mode。现在,您可以使用C-jC-k切换(和预览)缓冲区。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接