Say I have split my main Emacs Window into 2 : LEFT
and RIGHT
.
My cursor is currently in LEFT
. How do I copy some text from LEFT
to RIGHT
without going to the RIGHT
window?
Here is another opinionated implementation, it assumes 2 windows layout and isn't as sophisticated as append-to-buffer
, the point is Emacs is very easy to extend.
(defun my-copy-to-next-window (b e)
"Copy text in the region to next window."
(interactive "r")
(pcase (window-list)
(`(,w0 ,w1)
(with-selected-window w1
(insert-buffer-substring (window-buffer w0) b e)))
(t (user-error "Only works with 2 windows"))))
with-current-buffer
with with-selected-window
seems working).
Commented
Feb 3, 2019 at 6:41
There are a few relevant commands, though they are not bound to any keys by default. Try M-x append-to-buffer
to copy the region in the current buffer to another buffer.
See Accumulating-Text in the Emacs manual for details. Note these commands prompt for the target buffer. If you do this a lot with two windows open you could define your own command to look for the other visible buffer and call append-to-buffer
with that.
Unless this is something you do a lot, I think the simplest solution is to record a macro for copying to the other window.
Press f3
or C-x (
to start recording, then M-w C-x o C-y C-u - C-x o
to do the copying and come back, and f4
or C-x )
to stop recording. Then press f4
or C-x e
each time you want to copy something.
You may want to tweak the macro to leave the cursor in the other buffer or to move it in a certain way. The point is that you record whatever repetitive action you want, then press a single key to perform it again.
See the manual for more information, including how to work with more than one macro, how to bind a macro to a key and how to save macros.
One indirect approach came to my mind. This can be done using ediff-buffers
. When we run this command, it puts 2 buffers in vertically split windows. Then, using n
and p
keys, you can go to the next and previous difference. Then, by pressing a
or b
, you can copy-paste difference from one buffer to the other.
You call the buffer that's in the RIGHT window (C-x b) into the LEFT window. You paste what you need into the buffer, and you revert to the original buffer.
No need to change of window, you just change of buffer in the LEFT window.
C-x o
to jump between two buffers that are open. On the secondC-x o
you return to the same place you left. Depending on why you don't want to go to the other window you might be able to use my approach.