emacs 中缓冲区分割后如何切换焦点?

发布于 2024-11-16 17:48:31 字数 98 浏览 2 评论 0原文

我希望在分割窗口(Cx 3Cx 2)后能够自动到达新打开的缓冲区(当前缓冲区以外的缓冲区)中的光标。我怎样才能实现这种行为?

I would like that after splitting the window (C-x 3 or C-x 2) to be able to automatically get to cursor in the new opened buffer (the other than the current). How can I achieve this behavior ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

素衣风尘叹 2024-11-23 17:48:31

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

You can switch between buffers with C-x o. As to do that automatically I don't think there is an existing command for that.

旧时光的容颜 2024-11-23 17:48:31

您可以这样做:

(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,它就会起作用。

You can do it like this:

(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)))

In Emacs 24.3.1 it works if you change the argument 1 for 0.

小女人ら 2024-11-23 17:48:31

!!!不要使用这个答案!!! - 正如评论中指出的,建议 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))

!!!DO NOT USE THIS ANSWER!!! -- as pointed out in the comments, advising split-window can lead to undesired side-effects.

I recommend Bozhidar Batsov's answer instead.


Put the following in your .emacs file:

(defadvice split-window (after move-point-to-new-window activate)
  "Moves the point to the newly created window after splitting."
  (other-window 1))
箹锭⒈辈孓 2024-11-23 17:48:31

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

这可以通过更改用于分割窗口的函数来解决。默认情况下,它设置为 split-window-sensible,但您可以将其设置为您自己的函数,该函数调用 split-window-sensible,然后选择缓冲区。

但不幸的是,当您在迷你缓冲区中点击 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。)

As well as splitting the frame manually with C-x 2 or C-x 3, buffers are also automatically "popped-up" some times. These are also not selected/active by default.

This can be fixed by changing the function used to split a window. It's set to split-window-sensibly by default, but you can set it to your own function that calls split-window-sensibly and then selects the buffer.

Unfortunately, though, this has the side-effect of selecting the *Completions* buffer when you hit TAB in the minibuffer. So, it's worth checking to see if the minibuffer is active and not switching in this case. I'd bet there are other such undesirable scenarios as well. I'll try to update this post as and when I find them.

;; 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))))

(Works with Emacs 24.5.1.)

給妳壹絲溫柔 2024-11-23 17:48:31

我的想法是,当您想要在 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)

简单

My thought of when you would want to follow the window after a split-window was when it had the same buffer like in the following code:

(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)

Simple

临走之时 2024-11-23 17:48:31

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

C-x o will help you switch to the "other" buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文