118

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?

bstpierre
  • 30,042
  • 15
  • 70
  • 103
ant2009
  • 27,094
  • 154
  • 411
  • 609
  • This also works (tested in emacs24): [Transposing Two Buffers](http://emacswiki.org/emacs/SwitchingBuffers#toc7) It seems similar to *Bahbar* answer – nephewtom Feb 12 '15 at 12:10

10 Answers10

97

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.

Raja Selvaraj
  • 7,178
  • 3
  • 20
  • 13
  • This solution is perfect, and the comments are very clear - just make sure you read them :) – Martin Clarke Jun 09 '10 at 17:38
  • 3
    If you just want to swap windows (independent on the selected one), you can use the following `(defun win-swap () "Swap windows using buffer-move.el" (interactive) (if (null (windmove-find-other-window 'right)) (buf-move-left) (buf-move-right)))` – mefiX Nov 29 '10 at 12:00
  • buffer-move didn't work for me with the layout of just two windows but with `win-swap` it worked just fine, thanks! – Ev Dolzhenko Apr 02 '12 at 11:20
  • 2
    Inside that buffer-move source you'll see a comment about the all-important mappings for `C-S-up` to `buf-move-up` etc. – Micah Elliott Jul 11 '16 at 21:20
69

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?

dgtized
  • 3,192
  • 2
  • 24
  • 23
36

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            |      |                 |       |
       +-------------------------+      +-----------------+-------+
phils
  • 71,335
  • 11
  • 153
  • 198
  • 2
    `flop-frame` works only when the split between the windows is vertical, for horizontal split you need `flip-frame`. However, `rotate-frame` works irrespectively; one command to swap the buffers between two windows, no matter what the split orientation :) – legends2k Nov 28 '16 at 11:23
16

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.

smonff
  • 3,399
  • 3
  • 36
  • 46
zippy
  • 1,228
  • 10
  • 19
  • prelude looks like a mouthful (and it's not packaged anywhere - `curl | sh` = wtf?), but crux sure looks nice and does a bunch of stuff i rolled on my own in the first place. – anarcat Dec 10 '17 at 15:54
  • i know what `curl | sh` is, what i am saying is: it's evil. – anarcat Sep 06 '19 at 16:05
11

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

Bahbar
  • 17,760
  • 43
  • 62
  • I copied and pasted this code, and it doesn't seem to do anything. –  Nov 21 '09 at 08:48
  • Oh, sorry, it does do something, it swaps the top and bottom parts of the Emacs window. I was expecting it to swap the frames. –  Nov 21 '09 at 08:50
  • ok. You're confusing me. you mentioned C-x 3. This is to create 2 emacs windows, not 2 emacs frames. Are you using frames or windows ? What do you call windows and what do you call frames? – Bahbar Nov 21 '09 at 08:54
  • also, you did not talk about top and bottom parts. Do you have more than 2 buffers showing at once ? – Bahbar Nov 21 '09 at 08:55
  • I'm not the person who asked the question, I'm just an interloper. I had never used C-x 3 before I saw this question, but as you say it splits the Emacs window, rather than creating a new frame. –  Nov 21 '09 at 09:54
  • doh. Sorry. I should have noticed – Bahbar Nov 21 '09 at 13:22
  • Well, your answer seems to be correct! For some reason I am chronically incapable of understanding Lisp despite my efforts. –  Nov 21 '09 at 16:59
  • Sorry, I guess I was wrong. I guess I should have been using Windows instead of frames. – ant2009 Nov 22 '09 at 14:10
7

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

Rajanboy
  • 2,266
  • 2
  • 8
  • 15
2

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

phillc
  • 7,137
  • 1
  • 22
  • 15
  • For Spacemacs users, `ace-swap-window` is bound to `SPC w M`. This functionality is available by default. – Claudio Apr 12 '19 at 16:42
1

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"))
Aborn Jiang
  • 981
  • 10
  • 9
1

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

flexw
  • 63
  • 8
-1

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.

Michael H.
  • 862
  • 1
  • 7
  • 14