3

I'm not sure whether anyone has ever implemented what I'm looking for, but maybe I'm just using the wrong terms to search for it.

What I want to achieve is essentially a reverse copy and paste:

  • I'm in Buffer A, Position X
  • Hit "paste" (to mark where I want to paste stuff without actually pasting stuff)
  • Go to Buffer B, Position Y
  • Hit "copy"
  • Emacs jumps back to Buffer A, Position X and pastes whatever I copied before

I feel like this would a much quicker workflow than traditional copy-and-paste (where I have to manually jump back to where I was, especially if I have to look through a hand full of files before finding what I want to paste).

Is there a package for that? If not, whats a good starting point for my own macro?

Bonus points if it integrates nicely with the system clipboard or if there's a generic solution for Linux.

2

3 Answers 3

1

You can record locations in a register. That is:

  1. Record your current position in register 'a': C-x r <space> a
  2. Go copy something, as above
  3. Return to the recorded position with C-x r j a
  4. Paste your copy.

If you do this a lot, you may want to define some functions to handle it for you:

(defun tws-save-point ()
    (interactive)
    (point-to-register ?q))

(defun tws-jump-to-saved-point ()
    (interactive)
    (jump-to-register ?q))

(global-set-key [f7] 'tws-save-point)
(global-set-key [f8] 'tws-jump-to-saved-point)

If you always want to paste something when you return to the saved location, you could use this instead:

(defun tws-jump-to-saved-point-and-yank ()
    (interactive)
    (jump-to-register ?q)
    (yank))
1

Have you tried window splitting?

First, navigate to where you want to yank (paste) into buffer A. Now set up your two buffers side-by-side (one-time task):

C-x 4 b <RET>
B <RET>
C-x o

Point is now back where you started in buffer A, with buffer B visible (but not selected) next to buffer A.

From now on you can do the following: switch to buffer B, copy what you need, switch back to buffer A, and yank. You probably want to record it as a keyboard macro to give you that one-keystroke automation you're looking for.

C-x o
M-w
C-x o
C-y

To make the M-w step more flexible, see (info "(emacs) Keyboard Macro Query") on how to use C-r to enter a recursive editing level during keyboard macro execution.

For those bonus points: here's how I integrate the Emacs kill-ring with X11 copy-and-paste on *n*x.

;; One or other or both? Not sure. I have both.
(setq 
  interprogram-cut-function 'x-select-text
  x-select-enable-clipboard t)
0

I don't know of a package which provides this functionality, no.

I dimly remember a package which provided a kind of "single click copy&paste" where IIRC a press+down+release of the mouse in one window would copy&paste the selected text to the position of the cursor in "the" other window. But I can't find it any more.

The operations you request are pretty easy to implement:

(defvar my--prepared-region
  (let ((ol (make-overlay (point) (point))))
    (delete-overlay ol)
    ol))

(defun my-prepare-for-paste (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end))))
  (move-overlay my--prepared-region
                (or beg (point)) (or end (point))))

(defun my-paste-into-prepared-region (string)
  (interactive
   (list (funcall region-extract-function nil)))
  (with-current-buffer (overlay-buffer my--prepared-region)
    (let ((delcount (- (overlay-end my--prepared-region)
                       (overlay-start my--prepared-region))))
      (goto-char (overlay-start my--prepared-region))
      (insert string)
      (delete-char delcount))))

The main question is one of UI: which key/mouse events would you bind this to (since using M-x ... for those would likely get annoying pretty quickly).

Your Answer

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

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