108
votes

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?

8
This also works (tested in emacs24): Transposing Two Buffers It seems similar to Bahbar answernephewtom

8 Answers

92
votes

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.

48
votes

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?

36
votes

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            |      |                 |       |
       +-------------------------+      +-----------------+-------+
16
votes

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.

11
votes

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

1
votes

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

0
votes

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"))
-1
votes

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.