如何在emacs中进行缓冲区拆分后切换焦点?

时间:2011-06-24 07:31:54

标签: emacs window split emacs23

我希望在拆分窗口(C-x 3C-x 2)之后能够自动获取新打开的缓冲区中的光标(当前除外)。我怎样才能实现这种行为?

6 个答案:

答案 0 :(得分:54)

您可以使用C-x o在缓冲区之间切换。至于自动执行此操作,我认为没有现成的命令。

答案 1 :(得分:25)

你可以这样做:

(global-set-key "\C-x2" (lambda () (interactive)(split-window-vertically) (other-window 1)))
(global-set-key "\C-x3" (lambda () (interactive)(split-window-horizontally) (other-window 1)))

在Emacs 24.3.1中,如果您更改1的参数0,它就会起作用。

答案 2 :(得分:13)

!!!请勿使用此答案!!! - 正如评论中指出的那样,建议split-window会导致不良副作用。

我建议 Bozhidar Batsov 的回答。


将以下内容放入.emacs文件中:

(defadvice split-window (after move-point-to-new-window activate)
  "Moves the point to the newly created window after splitting."
  (other-window 1))

答案 3 :(得分:1)

除了用 C-x 2 C-x 3 手动分割帧之外,缓冲区也会自动弹出"弹出"有时候。默认情况下,这些也未被选中/激活。

可以通过更改用于拆分窗口的功能来解决此问题。默认设置为split-window-sensibly,但您可以将其设置为调用split-window-sensibly的自己的函数,然后选择缓冲区。

不幸的是,当你在迷你缓冲区中点击 TAB 时,这会产生选择*Completions*缓冲区的副作用。因此,在这种情况下,检查迷你缓冲区是否处于活动状态并且切换是值得的。我还打赌还有其他类似的不良情景。当我找到它们时,我会尝试更新这篇文章。

;; after splitting a frame automatically, switch to the new window (unless we
;; were in the minibuffer)
(setq split-window-preferred-function 'my/split-window-func)
(defun my/split-window-func (&optional window)
  (let ((new-window (split-window-sensibly window)))
    (if (not (active-minibuffer-window))
        (select-window new-window))))

(适用于Emacs 24.5.1。)

答案 4 :(得分:0)

我想到了split-window之后你想要跟随窗口的时间是什么时候它有相同的缓冲区,如下面的代码所示:

(defun split-window--select-window (orig-func &rest args)
  "Switch to the other window after a `split-window'"
  (let ((cur-window (selected-window))
        (new-window (apply orig-func args)))
    (when (equal (window-buffer cur-window) (window-buffer new-window))
      (select-window new-window))
    new-window))
(advice-add 'split-window :around #'split-window--select-window)

简单

答案 5 :(得分:-1)

C-x o 将帮助您切换到“其他”缓冲区。