How to swap buffers in 2 emacs windows

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

For example: I open 1 file using Cx Cf file1.c RET

and I divided the frame into two windows: Cx 3

Then I open another file Cx Cf 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 for file sharing? Usually I like to work in the left window when I have 2 windows. I know that I can easily do Cx o to move the cursor to the desired window.

However, I'm just wondering if I can change the files so that file2.c in the left window and file1.c is in the right window?

+81
emacs
Nov 21 '09 at 7:30
source share
10 answers

I am using buffer-move for this. Now, if you are working on a buffer on the left side, calling "buf-move-right" will replace it with the right. I think this is what you want.

+74
Nov 22 '09 at 12:37
source share

The transpose-frame library provides a fairly complete set of functions for switching or rotating window devices in frames.

Mx flop-frame RET does just that.

The following diagrams are from comments in the library (and on the 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 | | | | +-------------------------+ +-----------------+-------+ 
+27
May 11 '12 at 6:46 am
source share

If you use Prelude , you can simply use Cc s ( prelude-swap-windows ). From the Prelude documentation:

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

+12
04 Oct '13 at 14:03
source share

I do not know any built-in function that does this.

However, this does not seem too complicated to crack some elisp for this. The devil is in the details.

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

Remarkably, this cannot do what you want with regard to where the carriages end. However, first you need to say what you want: p

+11
Nov 21 '09 at 8:37
source share

The Emacs 26.1 NEWS file has the following entry:

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

Which seems to offer similar functionality for crux-transpose-windows but can also do some height / width transposition?

+5
Nov 20 '17 at 0:44
source share

If you have a prelude, you can use ace-window with Sw . 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 remove or replace, etc. The default bindings are:

x - remove window

m - swap window (move)

c - split the window right, vertically or horizontally

v - split the window vertically

b - horizontal window splitting

n - select the previous window

...

So it will be Sw m

+1
Oct 19 '17 at 13:52 on
source share

This also works (tested in emacs24): Transposing two buffers

Seems like Bahbar's answer

0
Feb 12 '15 at 12:10
source share

The following code fragment can execute a 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")) 
0
Nov 28 '15 at 13:07 on
source share

Here is an easy way.

 (defun flip-windows (&optional arg) "Flip two buffers" (interactive) (let* ((curr (current-buffer)) (other (other-buffer curr t))) (progn (switch-to-buffer other nil t) (switch-to-buffer-other-window curr)))) 

When you have two windows open, the behavior is pretty obvious. If there are more than two, it may be the choice of a “different” buffer that you do not expect.

0
Feb 04 '19 at 15:53
source share

I would try to open file # 2 in the right place, i.e. after you press cx 3, move the cursor with cx o before moving to the second file.

-one
Nov 21 '09 at 8:02
source share



All Articles