Copy And Paste

Emacs has its own terminology and keys for these concepts:

Common NameCommon KeyEmacs NameEmacs Key
Selection Region
Clipboard Kill ring
Cut C-x Kill C-w
Copy C-c kill-ring-save M-w
Paste C-v Yank C-y
yank-pop M-y

Changing the defaults

If you do not care for the DefaultKillingAndYanking key bindings, then consider these alternatives:

X Window System

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:

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 "") '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)

Wayland

Although Emacs 29 has an option to build with pure GTK and therefore is supposed to support Wayland clipboard natively, that support does not work if emacs is run in a tty, or when run 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)

Third party plugins

simpleclip

More specifically, for copy&paste, there are only two commands:

simpleclip-get-contents simpleclip-set-contents

cliphist

Read clipboard history from clipboard managers (Parcellite, ClipIt at Linux and Flycut at Mac). https://github.com/redguardtoo/cliphist

datclip

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.

clipmon

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.

XEmacs

(setq interprogram-cut-function 'own-clipboard) (setq interprogram-paste-function 'get-clipboard)
Редактировалось последний раз 2024-01-04 09:34 UTC пользователем benjamb (изменения)

CC-GNU GPL

This work is licensed to you under version 2 of the GNU General Public License. Alternatively, you may choose to receive this work under any other license that grants the right to use, copy, modify, and/or distribute the work, as long as that license imposes the restriction that derivative works have to grant the same rights and impose the same restriction. For example, you may choose to receive this work under the GNU Free Documentation License, the CreativeCommons ShareAlike License, the XEmacs manual license, or similar licenses.