Reputation: 22686
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?
Upvotes: 120
Views: 40305
Reputation: 2682
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
Upvotes: 17
Reputation: 73
If you use Ace window, which I highly recommend, you can swap buffers with ace-swap-window
.
Upvotes: 2
Reputation: 3381
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?
Upvotes: 73
Reputation: 7471
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
Upvotes: 2
Reputation: 1258
If you are using Prelude you can just use C-c s
(prelude-swap-windows
). From the Prelude documentation:
C-c s
runs the commandcrux-swap-windows
(found inprelude-mode-map
), which is an alias forcrux-transpose-windows
in crux.el.
Upvotes: 16
Reputation: 1059
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"))
Upvotes: 1
Reputation: 73355
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 | | | |
+-------------------------+ +-----------------+-------+
Upvotes: 36
Reputation: 7218
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.
Upvotes: 101
Reputation: 18015
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
Upvotes: 11
Reputation: 902
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.
Upvotes: -1