How to swap the buffers in 2 windows emacs

我使用 emacs 我发现有时我有2个文件分成2个窗口。

For example: 我使用 C-x C-f file1.c RET打开1个文件

我把画面分成两个窗口: C-x 3

I then open another file C-x C-f file2.c RET

我有两个文件:

窗口1(左) file1.c

窗口2(右) file2.c

我想知道是否有任何密钥组合来交换文件?通常当我有两个窗口时,我喜欢在左边的窗口工作。我知道我可以很容易地做 C-x o移动光标到正确的窗口。

However, I am just wondering if I can swap the files so that file2.c is in the left window and file1.c is in the right window?

35278 次浏览

我将设法在所需的位置打开文件 # 2,即在点击 c-x3之后,在导航到第二个文件之前用 c-xo 移动光标。

我不知道有什么内置函数可以做到这一点。

However, it does not seem too difficult to whip up some elisp for doing it. Devil is in the details though.

(defun swap-buffers-in-windows ()
"Put the buffer from the selected window in next window, and vice versa"
(interactive)
(let* ((this (selected-window))
(other (next-window))
(this-buffer (window-buffer this))
(other-buffer (window-buffer other)))
(set-window-buffer other this-buffer)
(set-window-buffer this other-buffer)
)
)

值得注意的是,关于插入符号的结束位置,这可能不是您想要的结果。 但是,您首先必须说出您想要的: p

我用的是 缓冲移动。现在,如果您正在处理左侧的缓冲区,调用“ buf-move-right”会将其与右侧的缓冲区交换。我想这就是你想要的。

转换框架库提供了一组相当全面的函数,用于翻转或旋转框架中的窗口排列。

M-x flop-frame RET做这个特殊的问题需要。

以下图表来自图书馆的注释(及其 EmacsWiki 页面) :

‘transpose-frame’ … Swap x-direction and y-direction


+------------+------------+      +----------------+--------+
|            |     B      |      |        A       |        |
|     A      +------------+      |                |        |
|            |     C      |  =>  +--------+-------+   D    |
+------------+------------+      |   B    |   C   |        |
|            D            |      |        |       |        |
+-------------------------+      +--------+-------+--------+


‘flip-frame’ … Flip vertically


+------------+------------+      +------------+------------+
|            |     B      |      |            D            |
|     A      +------------+      +------------+------------+
|            |     C      |  =>  |            |     C      |
+------------+------------+      |     A      +------------+
|            D            |      |            |     B      |
+-------------------------+      +------------+------------+


‘flop-frame’ … Flop horizontally


+------------+------------+      +------------+------------+
|            |     B      |      |     B      |            |
|     A      +------------+      +------------+     A      |
|            |     C      |  =>  |     C      |            |
+------------+------------+      +------------+------------+
|            D            |      |            D            |
+-------------------------+      +-------------------------+


‘rotate-frame’ … Rotate 180 degrees


+------------+------------+      +-------------------------+
|            |     B      |      |            D            |
|     A      +------------+      +------------+------------+
|            |     C      |  =>  |     C      |            |
+------------+------------+      +------------+     A      |
|            D            |      |     B      |            |
+-------------------------+      +------------+------------+


‘rotate-frame-clockwise’ … Rotate 90 degrees clockwise


+------------+------------+      +-------+-----------------+
|            |     B      |      |       |        A        |
|     A      +------------+      |       |                 |
|            |     C      |  =>  |   D   +--------+--------+
+------------+------------+      |       |   B    |   C    |
|            D            |      |       |        |        |
+-------------------------+      +-------+--------+--------+


‘rotate-frame-anti-clockwise’ … Rotate 90 degrees anti-clockwise


+------------+------------+      +--------+--------+-------+
|            |     B      |      |   B    |   C    |       |
|     A      +------------+      |        |        |       |
|            |     C      |  =>  +--------+--------+   D   |
+------------+------------+      |        A        |       |
|            D            |      |                 |       |
+-------------------------+      +-----------------+-------+

如果你使用的是 前奏曲,你可以直接使用 C-c s(prelude-swap-windows):

C-c s运行命令 crux-swap-windows(在 prelude-mode-map中找到) , 这是 Crux.elcrux-transpose-windows的别名。

下面的代码片段可以执行开关缓冲区。

(defun ab/switch-buffer-each-other (arg)
"switch current buffer with other window buffer
right-2-left and up-2-down"
(interactive "p")
(cond
((windmove-find-other-window 'right) (buf-move-right))
((windmove-find-other-window 'left) (buf-move-left))
((windmove-find-other-window 'up) (buf-move-up))
((windmove-find-other-window 'down) (buf-move-down)))
(message "switch buffer done"))

如果你有前奏,你可以使用与 S-w的王牌窗口。从那里你可以做很多事情列在他们的 医生

也可以从调用 ace-window 开始,然后决定切换 删除或交换等操作。默认情况下,绑定是:

X-delete 窗口

M- 交换(移动)窗口

c - split window fairly, either vertically or horizontally

垂直分割窗口

水平分割窗口

N-选择上一个窗口

...

所以应该是 S-w m

In the Emacs 26.1 NEWS file there is the following entry:

+++
*** New command 'window-swap-states' swaps the states of two live
windows.

它似乎提供了类似于 crux-transpose-windows的功能,但也可以做一些高度/宽度的转换?

如果您使用我强烈推荐的 Ace window,您可以与 ace-swap-window交换缓冲区。

As current emacs 29 version. 我们有 windmove,它就像魔术般的交换窗口。

windmove-swap-states-right windmove-swap-states-up windmove-swap-states-left windmove-swap-states-down