How to swap the buffers in 2 windows emacs

Solution 1:

I use buffer-move for this. Now if you are working on the buffer on the left side, calling 'buf-move-right' will swap it with the one on the right. I guess this is what you want.

Solution 2:

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

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

Which appears to offer similar functionality to crux-transpose-windows but can also do some height/width transpositions?

Solution 3:

The transpose-frame library provides a pretty comprehensive set of functions for flipping or rotating the window arrangements in frames.

M-x flop-frame RET does what this particular question needs.

The following diagrams are from the commentary in the library (and its EmacsWiki page):

‘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            |      |                 |       |
       +-------------------------+      +-----------------+-------+

Solution 4:

If you are using Prelude you can just use C-c s (prelude-swap-windows). From the Prelude documentation:

C-c s runs the command crux-swap-windows (found in prelude-mode-map), which is an alias for crux-transpose-windows in crux.el.