5

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?

2

2 Answers 2

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.

6
  • 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. Commented Jan 11, 2021 at 8: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. Commented Jan 11, 2021 at 8:13
  • that superuser answer is incomplete, I found this limitation when I tested it: superuser.com/questions/68170/… . Can you please check if the same happens for you? Commented Jan 12, 2021 at 6:27
  • @PeeyushKushwaha Yep, I did exhaustively check the Autocutsel solution (by looking at Vim registers) and it worked flawlessly.
    – Quasímodo
    Commented Jan 12, 2021 at 11:30
  • 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
    Commented Jan 13, 2021 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .