4

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.

1 Answers1

2

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.

Quasímodo
  • 18,865
  • 4
  • 36
  • 73
  • Actually Qt and GDK do use X resources (have "always" done so), but they don't use the X Toolkit library to access them (XGetDefault, for instance). If they didn't use X resources, they would be far more limited than they are. In the particular case you're mentioning though, it's mostly hard-coded stuff (very limited in comparison with other approaches). – Thomas Dickey Apr 13 '21 at 21:20
  • @Quasímodo, then it seems that I would be better off by just configuring everything through CLIPBOARD. Is there a way to disable copy to PRIMARY? or at least make it so that middleclicking pastes from CLIPBBOARD rather than PRIMARY. I dont want both selections fighting for dominance and keybindings – barzilay Apr 13 '21 at 21:28
  • @barzilay See if you want to fuse PRIMARY and CLIPBOARD or maybe https://unix.stackexchange.com/q/213840. If neither is what you want, I'd recommend exploring the options of some clipboard managers as many allow a special handling of Clip and Primary. – Quasímodo Apr 13 '21 at 21:58
  • @Quasímodo, Sadly neither of those work for me. If i only fuse them, then selecting text will pollute the shared clipboard, if there is a way to disable copying to PRIMARY then this would work for me. I'm trying a few clipboard managers but none of them so far can completely block copy to PRIMARY :/ but thank you for your time – barzilay Apr 13 '21 at 22:23
  • The X resources are the same. I happened to recall this because I documented that stuff in Xcursor recently. X Toolkit just happens to have a nice interface to that stuff, but Qt and GDK can't do without it. – Thomas Dickey Apr 13 '21 at 22:42
  • X Toolkit uses this part of Xcursor's API. Qt and GDK use other parts (via libraries lacking documentation...). But Xcursor uses X resources for either route. – Thomas Dickey Apr 13 '21 at 22:48
  • The translations resource of course is specific to X Toolkit. The answer which you linked to doesn't mention that detail, but at the end where it says "Most Gtk applications ignore X resources," that's incorrect. Which resources they use depends on the libraries (hint: Xft is a library), but they all use *some* X resources. – Thomas Dickey Apr 13 '21 at 23:05
  • @barzilay You're welcome. On the bright side, I guess to disable Primary is almost the same as not using it? Of course, feel free to open a new question, but as far as I know and where my research led, it is not possible to disable Primary. – Quasímodo Apr 14 '21 at 14:24
  • @Quasímodo, Yeah, it doesn't seem to be possible to disable PRIMARY... On Wayland on the other-hand is another story, but I'm stuck with Xorg for the foreseeable future because of nvidia. And about not using PRIMARY, the thing is that I do want to use PRIMARY! I love being able to paste by middleclicking, but it just doesnt work for me if what I want to paste is always being overwritten by stuff that I mistakenly highlighted – barzilay Apr 14 '21 at 22:08
  • @barzilay Well, then you can only use the clipboard and assign middle-click to paste from clipboard. That is totally doable, in case you're interested let me know. – Quasímodo Apr 15 '21 at 14:27
  • @Quasímodo, yes I am! how can I do that? would it break other features like middle-clicking a link to open it in a new tab? Also, is there a way to make something like copying into clipboard by middle-cliking highlighted text? or any other mousekeys combination if middle-clicking cant be used for both copying and pasting – barzilay Apr 15 '21 at 20:05
  • 1
    See https://askubuntu.com/q/276975. All you need to do is to assign one of those commands to middle-click in your window manager. But yeah, that will break middle-clicking links. Likewise, you could assign a right click to copy to clipboard, but again this will break other functionalities that require right clicks. It's all about avoiding conflicts. In case you have trouble but find the issue worth pursuing, I advise you to open a new question and to make your requirements as clear as possible upfront. – Quasímodo Apr 16 '21 at 12:38