macosemacscopy-paste

How to paste with mouse middle click the text copied from Emacs or other applications?


I would like to:

  1. Copy text from Emacs by highlighting it with a mouse (or copy from other applications with command+c).
  2. Paste the copied text into Emacs with mouse middle click (or paste it into other applications with command+v).

How can I do this?

Copying from Emacs and then pasting into Emacs works OK.
But copying from other applications and pasting into Emacs with mouse middle click does not work. I have to use control+y instead.

I am using Emacs Version 30.1 from GNU Emacs For Mac OS X, running on MacBook Pro with Apple M1 Max chip, macOS Sequoia 15.4.1.

Notes

I also tried using the code below in my ~/.emacs file:

(setq select-active-regions nil)
(setq mouse-drag-copy-region t)
(global-set-key [mouse-2] 'mouse-yank-at-click)

But this code has a side effect described in this post: After cutting text, and then copying another text, the cut text is prepended to the copied text upon pasting - Emacs Stack Exchange. The issue described in that post still remains unsolved. I borrowed this code from this answer to this question: How to combine Emacs primary/clipboard copy and paste behavior on MS Windows? - Stack Overflow.


Solution

  • If you want to make mouse middle-click paste from the clipboard instead, regardless of whether the text came from Emacs or a macOS app, you can redefine the mouse-2 binding to yank from the clipboard, like this

    Add this to your ~/.emacs

    (defun my/mouse-yank-from-clipboard (event)
      "Paste text from the clipboard at mouse click."
      (interactive "e")
      (mouse-set-point event)
      (insert (gui-get-selection 'CLIPBOARD)))
    
    (global-set-key [mouse-2] #'my/mouse-yank-from-clipboard)
    

    This do as below

    Below is what happen in Emacs

    Note

    If you also want Emacs kill commands (like C-w, M-w) to copy to the system clipboard, add below.

    (setq select-enable-clipboard t)
    

    Update 1

    Can you use below. The code is updated with If Emacs has a primary selection, use it else use the system clipboard.

    (defun my/mouse-yank-smart (event)
      "Paste primary selection if available, otherwise paste from clipboard."
      (interactive "e")
      (mouse-set-point event)
      (if (and (fboundp 'gui-get-primary-selection)
               (gui-get-primary-selection))
          (insert (gui-get-primary-selection))
        (insert (gui-get-selection 'CLIPBOARD))))
    
    (global-set-key [mouse-2] #'my/mouse-yank-smart)
    

    Update 2

    Can you try updated code below for last issues.

    ;; Make all Emacs copy/cut operations go to system clipboard
    (setq select-enable-clipboard t)
    
    ;; Disable primary selection to avoid conflicts (no X11 primary selection on macOS)
    (setq select-enable-primary nil)
    
    ;; Make mouse-dragging a region immediately copy it to clipboard
    (setq mouse-drag-copy-region t)
    
    ;; Smart middle-click: paste active region if it exists, else paste from clipboard
    (defun my/mouse-yank-smart (event)
      "Paste active region if available, otherwise paste from system clipboard."
      (interactive "e")
      (mouse-set-point event)
      (cond
       ;; If there's an active region, paste it
       ((use-region-p)
        (insert (buffer-substring-no-properties (region-beginning) (region-end))))
       ;; Else paste from macOS system clipboard
       (t
        (insert (gui-get-selection 'CLIPBOARD)))))
    
    ;; Bind middle-click to our smart paste function
    (global-set-key [mouse-2] #'my/mouse-yank-smart)