How to swap the buffers in 2 windows emacs

Emacs

Emacs Problem Overview


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?

Emacs Solutions


Solution 1 - Emacs

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

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

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

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 5 - Emacs

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 6 - Emacs

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

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 8 - Emacs

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 9 - Emacs

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

Solution 10 - Emacs

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionant2009View Question on Stackoverflow
Solution 1 - EmacsRaja SelvarajView Answer on Stackoverflow
Solution 2 - EmacsdgtizedView Answer on Stackoverflow
Solution 3 - EmacsphilsView Answer on Stackoverflow
Solution 4 - EmacszippyView Answer on Stackoverflow
Solution 5 - EmacsBahbarView Answer on Stackoverflow
Solution 6 - EmacsphillcView Answer on Stackoverflow
Solution 7 - EmacsBecause i hate myselfView Answer on Stackoverflow
Solution 8 - EmacsAborn JiangView Answer on Stackoverflow
Solution 9 - EmacsflexwView Answer on Stackoverflow
Solution 10 - EmacsMichael H.View Answer on Stackoverflow