== Terminology ==

Emacs has its own terminology and keys for these concepts:

|| *Common Name* || *Common Key*                                     || *Emacs Name*                     || *Emacs Key*        ||
|| Selection     || —                                                || Region                           || —                  || 
|| Clipboard     || <kbd>s-v</kbd> (<kbd>s</kbd> is the Windows key) || Kill ring                        || —                  || 
|| Cut           || <kbd>C-x</kbd>                                   || Kill                             || <kbd>C-w</kbd>     ||
|| Copy          || <kbd>C-c</kbd>                                   || ##kill-ring-save##               || <kbd>M-</kbd>  ||
|| Paste         || <kbd>C-v</kbd>                                   || Yank                             || <kbd>C-y</kbd>     ||
|| —             || —                                                || ##yank-pop##                     || <kbd>M-y</kbd>     ||

== Changing the defaults ==

If you do not care for the DefaultKillingAndYanking [[key binding]]s, then consider these alternatives:

* PcSelectionMode -- the Windows and Motif flavour with <kbd>C-<ins></kbd>, <kbd>C-<del></kbd>, and <kbd>S-<ins></kbd>.
* CuaMode -- use <kbd>C-x</kbd>, <kbd>C-c</kbd> and <kbd>C-v</kbd> to copy and paste
* CuaLightMode -- something between PcSelectionMode and CuaMode
* DeleteSelectionMode -- <kbd>DEL</kbd> deletes the region; just typing replaces it.
* WholeLineOrRegion -- <kbd>C-w</kbd> and <kbd>M-w</kbd> act on the current line when TransientMarkMode is not active

== X Window System ==

Copy and paste support on the X window system (as used by Unix and Linux) has historically [http://standards.freedesktop.org/clipboards-spec/clipboards-latest.txt 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 <kbd>C-w</kbd> and <kbd>C-y</kbd> to use the primary selection.
* ##select-enable-clipboard## - default ##t##; set this to ##t## if you want the Emacs commands <kbd>C-w</kbd> and <kbd>C-y</kbd> 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)
== Wayland ==

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.

=== Pure GTK with primary clipboard disabled on system ===

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)
}}}


=== Pure GTK build with terminal or multiple displays ===

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)
}}}

== Third party plugins ==

=== simpleclip ===

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

=== 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)

---- 
CategoryEmulation
CategoryKeys
CategoryModes
CategoryRegion
