2

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?

1
  • Personally I just use C-x o to jump between two buffers that are open. On the second C-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.
    – Madsen
    Jan 18, 2019 at 8:00

5 Answers 5

4

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"))))
4
  • Your solution works perfectly. Thanks! However, when I have same buffer open in the 2 windows (with cursor at different positions), this fails. Running your command simply copies in the same window (since the other window is holding the same buffer). I need this to work as I have often a very large file opened side-by-side and I am copying text from one region to the other.
    – shivams
    Feb 3, 2019 at 5:54
  • @shivams That's expected, I knew such limitation when I wrote the command, I didn't try to resolve the issue since I won't use the command and I thought it's not very hard (replacing with-current-buffer with with-selected-window seems working).
    – xuchunyang
    Feb 3, 2019 at 6:41
  • @shivams I just edited the answer to fix the issue.
    – xuchunyang
    Feb 3, 2019 at 6:46
  • Thanks. Now this works perfectly as expected :)
    – shivams
    Feb 3, 2019 at 6:47
2

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.

2

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.

1
  • Using macros is an interesting idea :)
    – shivams
    Jan 18, 2019 at 9:20
1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.