I would like to:
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.
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.
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
Moves point where you click.
Inserts text from the clipboard (which macOS Cmd+C sets).
Avoids the messy primary selection issue entirely.
Works cleanly with macOS and Emacs for Mac.
Below is what happen in Emacs
mouse-2 (mouse-yank-at-click
) pastes from the primary selection.
On X11 systems (like Linux), selecting text with a mouse automatically sets the primary selection, but macOS doesn’t have this concept natively.
On macOS, when you copy text in another app (Cmd+C), it only goes to the clipboard, not the primary selection.
By default, Emacs on macOS integrates with the clipboard (for C-y and Cmd-v), but middle-click still tries to yank from the primary selection, which isn't getting updated from macOS apps.
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)
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)
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)