![[Home]](https://www.emacswiki.org/images/logo218x38.png)
Emacs has its own terminology and keys for these concepts:
| Common Name | Common Key | Emacs Name | Emacs Key |
| Selection | — | Region | — |
| Clipboard | s-v (s is the Windows key) | Kill ring | — |
| Cut | C-x | Kill | C-w |
| Copy | C-c | kill-ring-save | M- |
| Paste | C-v | Yank | C-y |
| — | — | yank-pop | M-y |
If you do not care for the DefaultKillingAndYanking key bindings, then consider these alternatives:
Copy and paste support on the X window system (as used by Unix and Linux) has historically been a mess. This is relevant, as Emacs supports the various aspects of this mess.
Important for this discussion is the understanding that X generally distinguishes between two types of selection, the PRIMARY and the CLIPBOARD. Every time you select a piece of text with the mouse, the selected text is set as the PRIMARY selection. Using the copy function will place the selected text into the CLIPBOARD. Pasting using the middle mouse button will insert the PRIMARY selection. Pasting using the yank/paste functions will insert the CLIPBOARD.
With this out of the way, starting with Emacs 24.1, GNU Emacs should already do the right thing here. If you dislike this behavior, there are two options you can customize:
select-enable-primary - default nil; set this to t if you want the Emacs commands C-w and C-y to use the primary selection.select-enable-clipboard - default t; set this to t if you want the Emacs commands C-w and C-y to use the clipboard selection.Yes, you can have Emacs use both at the same time.
This does not affect pasting using the middle mouse button. By default, this uses mouse-yank-primary, which will only look at the PRIMARY selection. If you want the middle mouse button to insert the CLIPBOARD instead, use the following:
(global-set-key (kbd "<mouse-2>") 'clipboard-yank) ;; before Emacs 25 it was called 'x-clipboard-yank
Finally, in other applications, pasting usually replaces the selected text with the contents of the clipboard. To enable this behavior in Emacs, use DeleteSelectionMode with the following:
(delete-selection-mode)
Although Emacs 29 has an option to build with pure GTK and therefore is supposed to support Wayland clipboard natively, it may have issues depending on configuration of the system or the environment Emacs is being run in.
When Emacs is built with pure GTK, if the primary clipboard is disabled in the system as a whole, then pasting will work as expected, but copying from Emacs to the clipboard will only give a blank entry there. This behaviour happens even when Emacs’s primary clipboard is disabled and can be corrected as follows:
;; credit: Lukas Barth at https://www.lukas-barth.net/blog/emacs-wsl-copy-clipboard/
(setopt select-active-regions nil)
It may also be necessary to make sure that select-enable_clipboard, select-enable-primary, and interprogram-cut-function are at their default settings:
(setopt select-enable-clipboard 't) (setopt select-enable-primary nil) (setopt interprogram-cut-function #'gui-select-text)
The support also does not work if Emacs is run in a terminal (tty), or inside multiple displays. For that to work, the wl-clipboard program is needed (you need to install wl-clipboard first):
;; credit: yorickvP on Github (setq wl-copy-process nil) (defun wl-copy (text) (setq wl-copy-process (make-process :name "wl-copy" :buffer nil :command '("wl-copy" "-f" "-n") :connection-type 'pipe :noquery t)) (process-send-string wl-copy-process text) (process-send-eof wl-copy-process)) (defun wl-paste () (if (and wl-copy-process (process-live-p wl-copy-process)) nil ; should return nil if we're the current paste owner (shell-command-to-string "wl-paste -n | tr -d \r"))) (setq interprogram-cut-function 'wl-copy) (setq interprogram-paste-function 'wl-paste)
You can use https://github.com/rolandwalker/simpleclip which ALWAYS works.
More specifically, for copy&paste, there are only two commands:
simpleclip-get-contents simpleclip-set-contents
Read clipboard history from clipboard managers (Parcellite, ClipIt at Linux and Flycut at Mac). https://github.com/redguardtoo/cliphist
If it’s getting to be a bit of a hassle, use https://github.com/thomp/datclip to simply show the primary, secondary, and clipboard selections in the datclip buffer.
Monitor the clipboard and insert any change into the kill-ring. It makes it easier to use yank-pop from several inputs outside Emacs. https://github.com/bburns/clipmon
Quick note: As of July 2023, because of this issue: https://github.com/bburns/clipmon/issues/3, clipmon may cause emacs to hang if you also use gui dialog boxes. obar’s suggestion re: copyq in a comment regarding that issue provides a simple and effective alternative for integrating the clipboard history with the kill-ring. Indeed, it was the only workable method that I could find. I could not get either gpaste/gpastel or cliphist to work for me.
(setq interprogram-cut-function 'own-clipboard) (setq interprogram-paste-function 'get-clipboard)