'How to swap the buffers in 2 windows emacs

I am using emacs I find that sometimes I have 2 files separated into 2 windows.

For example: I open 1 file using C-x C-f file1.c RET

and I split the frame into two windows: C-x 3

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

So I have 2 files:

window 1 (left) file1.c

window 2 (right) file2.c

I am wondering if there is any key combination to swap the files over? Normally I like to work on the left window when I have 2 window. I know I can easily do C-x oto move the cursor to the right window.

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?



Solution 1:[1]

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 2:[2]

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 3:[3]

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.

Solution 4:[4]

I'm not aware of any built-in function doing this.

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

Notably, this may not be doing what you desire with respect to where the caret ends up. However, you'd first have to say what you want :p

Solution 5:[5]

As current emacs 29 version. We have windmove , its like magic of swapping windows.

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

Solution 6:[6]

If you have prelude, you can use ace-window with S-w. From there you can do many things listed in their docs.

You can also start by calling ace-window and then decide to switch the action to delete or swap etc. By default the bindings are:

x - delete window

m - swap (move) window

c - split window fairly, either vertically or horizontally

v - split window vertically

b - split window horizontally

n - select the previous window

...

So it would be S-w m

Solution 7:[7]

The following code snippet can do switch buffer.

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

Solution 8:[8]

If you use Ace window, which I highly recommend, you can swap buffers with ace-swap-window.

Solution 9:[9]

I would contrive to open up file #2 in the desired location, i.e. after you hit c-x 3, move the cursor with c-x o before navigating to the second file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dgtized
Solution 2
Solution 3 smonff
Solution 4 Bahbar
Solution 5 Because i hate myself
Solution 6 phillc
Solution 7 Aborn Jiang
Solution 8 flexw
Solution 9 Michael H.