Does anyone know a command or somtething to copy an rectangular selection (as with C-x r M-w / copy-rectange-as-kill) and put the killed rectangle into the system clipboard like with the kill-ring-save command ?

I would something like : 1) copy the rectangle region 2) paste the rectangle in a temporary buffer 3) delete the trailing whitespaces 3) copy the buffer's content to system clipboard but not in the kill-ring (don't mess with the kill-ring) 4) delete the temporary buffer.

2 Answers 2

(defun copy-rectangle-to-system-clipboard (start end)
  "Like `copy-rectangle-as-kill', but also copy to system clipboard."
  (interactive "r")
  (call-interactively #'copy-rectangle-as-kill)
    (funcall interprogram-cut-function (buffer-string))))

I have tested it using graphical Emacs 25.2 on macOS.

  • FYI, (kill-new (buffer-string)) worked for me, but (funcall interprogram-cut-function (buffer-string)) does not. Emacs version: 28.1 Sep 7, 2022 at 19:18

Apart from deleting trailing whitespace, you get all you need with built-in CUA minor mode. Set it permanently with (cua-mode t) in your emacs config or toggle it interactively. Press C-<Return> to enter rectangle editing, use arrow keys to select the area, and copy it to kill-ring and system clipboard with C-c.

If you do a lot of copying and pasting, have a look at counsel-yank-pop (part of abo-abo's ivy/councel/swiper) that gives you an easy access to emacs kill-ring history. I've bound it to M-y. It makes it convenient to copy several chunks of text from your source and paste them one by one to your target document.

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.