14

Say we want to kill the region selected by all cursors in multiple-cursors, and yank it all to a new, empty buffer. How do you do that with multiple-cursors?

Note that a simple copy and paste of the text on the new buffer does not work, since it only yanks the region from the first cursor (as reported here).

Note: This question is motivated by the answer and problem presented in: Keeping only the lines with the cursors in multiple-cursors

2 Answers 2

14

Once text has been killed, exit multiple-cursor (enter) and while in new buffer hit C-x r y (normally the killed text is in the rectangle kill-ring)

3
  • 5
    After thousands of time failing to yank all the killed things I am frustrated to find that one MUST exit multiple-cursor to make C-x r y work. :( Commented Jan 6, 2016 at 16:08
  • The killed text in the first time persists somehow, is there something like a "cache" that I need to clear? Thanks!
    – benjaminz
    Commented Dec 22, 2016 at 20:43
  • 2
    I consider this solution to be ugly hack.
    – dev1223
    Commented Jul 6, 2019 at 19:36
0

Try this. Just select text using multiple cursors and hit C-c. Just like in any other normal editor these days.

(setq *cutom/bulk-clipboard* nil)

(mc/load-lists)
(push 'custom/copy-across-cursors
      mc/cmds-to-run-once)
(push 'cua-copy-region
      mc/cmds-to-run-once)


(defun custom/copy-across-cursors ()
  (interactive)
  (mc/save-excursion
   (mc/save-window-scroll
    (mc/for-each-fake-cursor
     (save-excursion
       (custom/copy-across-cursor cursor)))))
  (mc--reset-read-prompts)
  (push (buffer-substring
         (caar (region-bounds))
         (cdar (region-bounds)))
        *cutom/bulk-clipboard*)
  (kill-new (s-chomp (loop for x in *cutom/bulk-clipboard*
                           concat (concat x "\n"))))
  (setq *cutom/bulk-clipboard* nil))

(defun custom/copy-across-cursor (cursor)
  (let ((mc--executing-command-for-fake-cursor t)
        (id (overlay-get cursor 'mc-id))
        (annoying-arrows-mode nil)
        (smooth-scroll-margin 0))
    (mc/add-fake-cursor-to-undo-list
     (mc/pop-state-from-overlay cursor)
     (ignore-errors
       (custom/cursor-handle-copy)
       (mc/create-fake-cursor-at-point id)))))

(defun custom/cursor-handle-copy ()
  (when (region-active-p)
    (push (buffer-substring
           (caar (region-bounds))
           (cdar (region-bounds)))
          *cutom/bulk-clipboard*))
  (when deactivate-mark (deactivate-mark)))


(advice-add #'cua-copy-region
            :around (lambda (oldfn &rest args)
                      (if (> (mc/num-cursors) 1)
                          (custom/copy-across-cursors)
                        (apply oldfn args))))

Please, note that dependency on library s is present. Also this code expects you use CUA mode. If not, you must customize advice definition at the bottom.

1
  • didn't work for me. After I select the regions, enter CUA mode, a command buffer just loads, even calling custom/copy-across-cursors I get an error marker does not point anywhere
    – leinad
    Commented Jan 8 at 10:02

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.