如何从终端缓冲区切换到不同的缓冲区

70

我已�使用emacs几周了,到目�为止一切都很好——�vim过�比我预期的更容易了(�际上,emacs的键盘快�键感觉更自然)。

我添加了一些定制,比如使用 M-Left/Right/Up/Down 在缓冲区之间移动,因为当我一次打开四个文件时,C-x o 感觉有点慢。

到目�为止,都还�错😊

但是有一件事情困扰�我:

  1. 我使用 C-x 3 和 C-x 2 打开一些窗�。
  2. 我在其中一个中打开终端,使用 M-x term ENT
  3. 我如何使用键盘切�到��的窗格?

通常的快�键显然�起作用——终端会拦截�个emacs命令,我必须�击��的缓冲区�能使其生效。


6
赞成,被拯救的罪人在 Emacs 教堂尤为受欢迎。 - High Performance Mark
你应该考虑使用iswitch-buffer来更改缓冲区,而不是使用多个窗口。这样会大大提高生产力。 - Noufal Ibrahim
2
Emacs中的“windows”是在执行“C-x 2”等操作时获得的内容。在Emacs中,顶层应用程序窗口称为“Frames”。 - Noufal Ibrahim
1
@NoufalIbrahim 为什么不两者兼备呢?为了有效地工作,我经常需要同时查看两个文件或代码片段,使用两个窗口非常方便。然后使用 icomplete-mode 切换缓冲区(在24.4中替代了 iswitch-buffer:https://www.emacswiki.org/emacs/IswitchBuffers)。 - Aaron Wallentine
3
@ramy 我差不多10年前就问过那个问题了 :-) 现在我使用ace-window包快速跳转可见缓冲区。 - lukaszkorecki
显示剩余2条评论
5个回答

172
在 term-mode 中,任何正常的 C-x whatever 绑定键会变成 C-c whatever

1
这篇文章应该得到更多的赞同,有时候需要区分m-x term和m-x shell之间的差别——比如使用watch命令来监视变化的数据(除非有更好的emacs方法)。 - Danny Staple
非常正确,我的当前问题是我已经将窗口切换绑定到了C-f/b/n/p,但当我在term中时无法切换出去! - Chironex
7
有人知道为什么会这样吗?为什么不像平常一样使用C-x(stty -a命令未显示C-x的绑定)? - tjb
抱歉给你贡献负分。我看错了答案,以为它是错误的。不幸的是,我意识到得太晚,现在贡献已经被锁定。 - acorello
C-c shift-o 对我很有帮助。 - Pavan Ravipati

12

我不确定我理解你的问题。 如果你运行 M-x terminal,大部分键盘事件会被发送到底层终端,因此标准的 C-x o 绑定和你的 M-Left 在终端中不可用。

尝试使用 M-x shell 在一个窗口中获取一个shell,你设置的导航绑定应该仍然有效。


1
感谢 - 这解决了我的问题。我不知道 M-x shell 和 M-x term 之间有区别。 - lukaszkorecki

10
在终端模式下,输入C-c b RET以切换到其他缓冲区。这与通常情况下按C-x b RET所做的相同。

6

这个方法可以让C-x b生效。你可能需要添加任何自定义移动命令的绑定。

(add-hook 'term-mode-hook
   (lambda ()
     ;; C-x is the prefix command, rather than C-c
     (term-set-escape-char ?\C-x)
     (define-key term-raw-map "\M-y" 'yank-pop)
     (define-key term-raw-map "\M-w" 'kill-ring-save)))

顺便提一下,shell-mode和term-mode之间有很大的区别。前者与emacs更好地集成(例如cd命令)。后者是完整的终端仿真,并且可以处理curses程序。它们都有各自的用处。


2

如果要涉及到Emacs窗口的一般性答案,您可以查看 windmove,它开始在大约Emacs 22左右随着Emacs一起发布:

;;; Commentary:
;;
;; This package defines a set of routines, windmove-{left,up,right,
;; down}, for selection of windows in a frame geometrically.  For
;; example, `windmove-right' selects the window immediately to the
;; right of the currently-selected one.  This functionality is similar
;; to the window-selection controls of the BRIEF editor of yore.
;;
;; One subtle point is what happens when the window to the right has
;; been split vertically; for example, consider a call to
;; `windmove-right' in this setup:
;;
;;                    -------------
;;                    |      | A  |
;;                    |      |    |
;;                    |      |-----
;;                    | *    |    |    (* is point in the currently
;;                    |      | B  |     selected window)
;;                    |      |    |
;;                    -------------
;;
;; There are (at least) three reasonable things to do:
;; (1) Always move to the window to the right of the top edge of the
;;     selected window; in this case, this policy selects A.
;; (2) Always move to the window to the right of the bottom edge of
;;     the selected window; in this case, this policy selects B.
;; (3) Move to the window to the right of point in the selected
;;     window.  This may select either A or B, depending on the
;;     position of point; in the illustrated example, it would select
;;     B.
;;
;; Similar issues arise for all the movement functions.  Windmove
;; resolves this problem by allowing the user to specify behavior
;; through a prefix argument.  The cases are thus:
;; * if no argument is given to the movement functions, or the
;;   argument given is zero, movement is relative to point;
;; * if a positive argument is given, movement is relative to the top
;;   or left edge of the selected window, depending on whether the
;;   movement is to be horizontal or vertical;
;; * if a negative argument is given, movement is relative to the
;;   bottom or right edge of the selected window, depending on whether
;;   the movement is to be horizontal or vertical.

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