How to alter the trigger for copying something to the PRIMARY selection in X11

clipboardklipperx11

From my understanding, under X11 we have basically 2 ways to copy and paste stuff: the PRIMARY selection and the CLIPBOARD selection. Explicitly copying something with ctrl-c will utilize the clipboard selection and there are many applications that may manage your clipboard for you.

But what I'm interested in is altering the PRIMARY selection behavior, in particular I wish to make X11 stop automatically copying selected text to the primary selection.
My end goal is to be able to select text with my mouse and then middle-click with my mouse-wheel if I wish to copy it (or cut it if its easier to implement). I basically want to implement an extra step to copy stuff into PRIMARY.

I would also like to be able to paste this recently copied text by middle-clicking again the mouse. This is the default behavior for pasting the PRIMARY selection, so this should just work if we use the PRIMARY selection to copy stuff. (But if we somehow could remap everything to use CLIPBOARD we would gain the ability to paste images which I believe is impossible to do with PRIMARY)

What is the best way to accomplish this? Is this a xorg setting? Should I completely disable the PRIMARY selection and use some sort of keybinding application to implement this behavior with the CLIPBOARD selection? Is there a clipboard manager that can do this for me? I'm currently using Klipper, the clipboard manager that comes with KDE/plasma and this doesn't seem to be an option there.

Best Answer

My end goal is to be able to select text with my mouse and then middle-click with my mouse-wheel if I wish to copy it.

Each X11 application decides (and oftentimes hardcodes) what binding it uses to interact with Primary.

The traditional way to own Primary (as known as copy to Primary) is to simply make a selection and mouse-middle-click to paste from it. This was already the default for the Athena Widget Set (from the 80s, probably the first X widgets library), as you can see in its specification of the text widget. The binding can be altered for Athena and Motif applications via a translations X-resource (examples in Arch Wiki: XTerm and scrolling a single line in Xterm). The bad news is that such GUIs are legacy, and most modern X11 applications, based on Qt, GTK, etc., not only do not use a translations resource, but also oftentimes hardcode their keybidings.1

Moreover, although those Primary bindings are quite consolidated (the same for programs as old as XTerm to modern ones as Firefox), they are not written in stone. For a mouse-aware Ncurses program — e.g. Midnight Commander, Vim with mouse support enabled —, the copy binding is Shift+MouseSelection.2

Although nothing prevents a clipboard manager from snooping the Primary contents/ownership and controlling the Primary accordingly to arbitrary keybindings, in a manner similar to one described in Clipboard Managers section here, I don't think such a program exists yet.

Further reading:

1: For what it's worth, I have never seen a GTK or Qt application that would let me choose Primary keybindings.
2: Vim can copy to or paste from Primary with the keyboard only.

Related Question