4

If there weren't two different selections (primary and clipboard), then it would work exactly as I want. How this would work is:

  1. If anything wants to put text on primary or clipboard, the text gets put on both.
  2. When anything reads from either primary or clipboard, the same text would be read every time, thanks to #1.

Is this possible?

Quasímodo
  • 18,865
  • 4
  • 36
  • 73

2 Answers2

5

Shell script with Clipnotify

It is a program that simply exits whenever an X selection changes. So put it in a while loop and when it exits, figure out if the clipboard or the primary changed, and stuff the contents of the changed selection in the other selection.

get_selections_keep_newline() {
    c2=$(xsel -o --clipboard; printf x)
    c2=${c2%x}
    p2=$(xsel -o --primary  ; printf x)
    p2=${p2%x}
}

get_selections_keep_newline while clipnotify; do c1=$c2 p1=$p2 get_selections_keep_newline if [ "$c1" != "$c2" ]; then printf '%s' "$c2" | xsel -i --primary elif [ "$p1" != "$p2" ]; then printf '%s' "$p2" | xsel -i --clipboard fi done

Save it as mergexsel, make it executable and let it run.

The function contains a kludge to preserve trailing newline characters (abc\n is different from abc); other than that the script is self-explanatory.

Autocutsel

It must be run for both primary and clipboard (as this answer on Super User shows).

autocutsel -s CLIPBOARD &
autocutsel -s PRIMARY   & 

Diodon, a GUI clipboard manager

Let it run in the background with diodon &. Click the tray icon or issue another diodon command to pop up its GUI menu with the history of clipboard contents and the "Preferences" item. Select "Preferences" and in the window that opens, check the items

  • Use clipboard (Ctrl+C)

  • Use primary selection

  • Synchronize clipboards

Further reading: Clipboard — Arch Wiki for context and more clipboard managers.

Quasímodo
  • 18,865
  • 4
  • 36
  • 73
  • of all the options, diodon worked most flawlessly and it was indeed simple and minmal to configure. In particular, autocutsel failed (see this) maybe because it is meant to synchronize primary / clipboard to older ring buffers, but not synchronize primary and clipboard with eachother. I'd suggest removing it from your answer but leaving the arch-wiki link in. – Peeyush Kushwaha Jan 11 '21 at 08:12
  • Also, a note for future users of this answer: diodon works flawlessly on ubuntu 20.04 (installed from apt) but on ubuntu 20.10 it's a bit weird -- issuing the "diodon" command makes it pop-up a context menu from where you can select "preferences", but the command itself exits immediately. – Peeyush Kushwaha Jan 11 '21 at 08:13
  • that superuser answer is incomplete, I found this limitation when I tested it: https://superuser.com/questions/68170/how-can-i-merge-the-gnome-clipboard-and-the-x-selection#comment2465795_177541 . Can you please check if the same happens for you? – Peeyush Kushwaha Jan 12 '21 at 06:27
  • @PeeyushKushwaha Yep, I did exhaustively check the Autocutsel solution (by looking at Vim registers) and it worked flawlessly. – Quasímodo Jan 12 '21 at 11:30
  • Weird. I hope you did not have diodon running when you performed that check. I checked it by looking at xclip outputs and found what I mentioned in that comment. I guess I'll just leave this note here in case someone else encounters the same issue. – Peeyush Kushwaha Jan 13 '21 at 10:00
  • 1
    @PeeyushKushwaha I certified myself that Diodon was not running, but upon trying it again it seemed to me that a single time the contents were not synced, but it could be just my mind playing tricks on me, because I could not reproduce it again... – Quasímodo Jan 13 '21 at 15:18
0

This is an old question but I'm adding this in case someone else is looking. I get this result by using a gnome extension called "Pano" and one of its options in setrtings is to sync clipboards.