如何在2个窗口emacs中交换缓冲区

ant*_*009 91 emacs

我正在使用emacs,我发现有时候我将2个文件分成2个窗口.

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

我将框架分成两个窗口: C-x 3

然后我打开另一个文件 C-x C-f file2.c RET

所以我有2个文件:

窗口1(左) file1.c

窗口2(右) file2.c

我想知道是否有任何组合交换文件?通常情况下,当我有2个窗口时,我喜欢在左侧窗口上工作.我知道我可以轻松C-x o地将光标移动到右侧窗口.

但是,我只是想知道我是否可以交换文件,以便file2.c在左侧窗口中并且file1.c在右侧窗口中?

Raj*_*raj 86

我为此使用buffer-move.现在,如果您正在处理左侧的缓冲区,则调用"buf-move-right"会将其与右侧的缓冲区进行交换.我想这就是你想要的.

  • 如果您只想交换窗口(独立于所选窗口),您可以使用以下`(defun win-swap()"使用buffer-move.el交换窗口"(交互式)(if(null)(windmove-find-其他窗口'右))(buf-move-left)(buf-move-right)))` (3认同)
  • 在该缓冲区移动源中,您将看到有关“CS-up”到“buf-move-up”等所有重要映射的注释。 (2认同)

phi*_*ils 32

转置帧库提供了相当全面的函数集用于翻转或以帧旋转窗口安排.

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            |      |                 |       |
       +-------------------------+      +-----------------+-------+
Run Code Online (Sandbox Code Playgroud)

  • `flop-frame` 仅在窗口之间的拆分是垂直的时才起作用,对于水平拆分,您需要 `flip-frame`。然而,`rotate-frame` 无论如何都可以工作;无论拆分方向如何,都可以使用一个命令在两个窗口之间交换缓冲区:) (2认同)

dgt*_*zed 21

在Emacs 26.1 NEWS文件中,有以下条目:

+++
*** New command 'window-swap-states' swaps the states of two live
windows.
Run Code Online (Sandbox Code Playgroud)

哪个似乎提供类似的功能,crux-transpose-windows但也可以做一些高度/宽度换位?

  • 简单有效 (3认同)

zip*_*ppy 14

如果您使用Prelude,可以使用C-c s(prelude-swap-windows).来自Prelude文档:

C-c s运行命令crux-swap-windows(在发现prelude-mode-map),这是一个别名crux-transpose-windowscrux.el.


Bah*_*bar 11

我不知道有任何内置函数这样做.

然而,为了做到这一点,鞭挞一些elisp似乎并不太难.魔鬼虽然在细节.

(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)
    )
  )
Run Code Online (Sandbox Code Playgroud)

值得注意的是,对于插入符号的最终位置,这可能不是您所希望的.但是,你首先必须说出你想要的东西:p


Raj*_*boy 11

作为当前的 emacs 29 版本。我们有windmove,就像交换窗口的魔法一样。

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