Discussion:
sync X11 primary selection and secondary clipboard
(too old to reply)
Ben Collver
2022-05-07 15:29:34 UTC
Permalink
Hi,

I would like to synchronize the X11 primary selection and secondary
clipboard. My goal is to select text in rxvt and then use the
Edit/Paste menu to paste it into leafpad. I installed autocutsel
from slackbuilds.org because it looked promising. Then I tried the
following sequence of actions:

* Open rxvt
* Run: autocutsel -debug -verbose -selection PRIMARY
* Open leafpad
* Type text into leafpad: Howdy
* Press Ctrl-A to select the text
* Press Ctrl-X to cut the text
* Open another rxvt
* Run: uname -a
* Double-click to select the word Linux
* In leafpad, click Edit, Paste
It pastes: Howdy

I expected leafpad to paste "Linux".

When I run "cutsel cut" it outputs "Linux".
When I run "xclip -out -selection primary" it outputs "Linux".

When I run "cutsel sel" it outputs "Howdy".
When I run "xclip -out -selection clipboard" it outputs "Howdy".

Below is the text output from the autocutsel process:

bash-5.1$ autocutsel -debug -verbose -selection PRIMARY
autocutsel v0.10.1
Selection changed: "Howdy"
New value saved: "Howdy"
sel -> cut: "Howdy"
Updating buffer
Selection changed: "Linux"
New value saved: "Linux"
sel -> cut: "Linux"
Updating buffer

The second-to-the-last line of output led me to expect that
autocutsel copied the word Linux from the selection into the
clipboard, but on the contrary, the clipboard still contains the word
Howdy.

Can anyone recommend a different solution to synchronize my primary
selection to the secondary clipboard?

Thanks!
-Ben
Grant Taylor
2022-05-11 17:06:39 UTC
Permalink
Post by Ben Collver
Hi,
Hi,
Post by Ben Collver
I would like to synchronize the X11 primary selection and secondary
clipboard.
I'm not sure if you're wanting something that you manually initiate or
is automatically initiated in the background.

I use `xsel` do to this manually.
Post by Ben Collver
Can anyone recommend a different solution to synchronize my primary
selection to the secondary clipboard?
You seem to be missing a piece of the puzzle. X11 traditionally has
three buffers (in this context);

- primary
- secondary
- clipboard

Many GUI programs that originated on X11 use the clipboard. The primary
selection is usually incidentally altered as a precursor to altering the
clipboard.

I've only heard tell of one program, emacs, that actually utilizes the
secondary selection, save for utilities specifically meant work with
primary / secondary / clipboard.

XTerm uses the primary selection for it's copy and paste actions by default.

I'd encourage you to install, play with, and skim the man page for the
`xsel` command.

I've got the following aliases defined to make working with xsel more
convenient.

b2b='xsel -ob | xsel -ib' # remove formatting from clipboard
b2p='xsel -ob | xsel -ip' # copy clipboard to primary
ib='xsel -ib' # copy STDIN to clipboard
is='xsel -is' # copy STDIN to secondary
ob='xsel -ob' # copy clipboard to STDOUT
op='xsel -op' # copy primary to STDOUT
opf='xsel -op | fmt -w $COLUMNS' # copy primary to STDOUT via fmt
os='xsel -os' # copy secondary to STDOUT
p2b='xsel -op | xsel -ib' # copy primary to clipboard
p2p='xsel -op | xsel -ip' # remove formatting from primary
--
Grant. . . .
unix || die
Ben Collver
2022-05-23 00:22:20 UTC
Permalink
Post by Grant Taylor
I'm not sure if you're wanting something that you manually initiate or
is automatically initiated in the background.
You seem to be missing a piece of the puzzle. X11 traditionally has
three buffers (in this context);
- primary - secondary - clipboard
Thanks! I should have phrased it more carefully: I would like to
automatically synchronize the X11 primary and clipboard. Your b2p
and p2b aliases do what i want, except manually. This can also be
manually accomplished using xclip.

b2p='xclip -selection clipboard -out | xclip -selection primary -in'
p2b='xclip -selection primary -out | xclip -selection clipboard -in'

I had hoped that the autocutsel package would automate this process,
but it did not work as i expected.
Javier
2022-05-24 02:16:23 UTC
Permalink
Post by Ben Collver
Thanks! I should have phrased it more carefully: I would like to
automatically synchronize the X11 primary and clipboard. Your b2p
and p2b aliases do what i want, except manually. This can also be
manually accomplished using xclip.
b2p='xclip -selection clipboard -out | xclip -selection primary -in'
p2b='xclip -selection primary -out | xclip -selection clipboard -in'
I had hoped that the autocutsel package would automate this process,
but it did not work as i expected.
In xterm you can configure it to put the selected text automatically
on the clipboard.

From xterm(1)

selectToClipboard (class SelectToClipboard)
Tells xterm whether to use the PRIMARY or CLIPBOARD for SELECT
tokens in the selection mechanism. The set-select action can
change this at runtime, allowing the user to work with programs
that handle only one of these mechanisms. The default is
“false”, which tells it to use PRIMARY.


or enable the option in the VT menu accesed with Ctrl+middle mouse button
or enable it with XResources.

xterm -xrm '*selectToClipboard: true'

Or even synchronize both primary and clipboard with

xterm -xrm '*VT100*translations: #override \n\ <BtnUp> : select-end(PRIMARY, CLIPBOARD)'

rxvt as asked in the OP I don't believe it those options.
Ben Collver
2022-05-24 15:36:28 UTC
Permalink
Post by Javier
In xterm you can configure it to put the selected text automatically
on the clipboard.
Thanks! That looks useful for xterm. I that it is simple to configure.
I see that the urxvtperl man page mentions hooks for on_sel_grab() and
on_tt_paste(), which could be used to configure a similar behavior.

I will keep an eye out for something that can work system-wide.
Javier
2022-05-26 03:20:02 UTC
Permalink
Post by Ben Collver
Post by Javier
In xterm you can configure it to put the selected text automatically
on the clipboard.
Thanks! That looks useful for xterm. I that it is simple to configure.
I see that the urxvtperl man page mentions hooks for on_sel_grab() and
on_tt_paste(), which could be used to configure a similar behavior.
I will keep an eye out for something that can work system-wide.
I didn't know urxvtperl existed. It looks much more powerful than
xterm '*VT100*translations', which only gives access to a few predefined
functions.

I was just checking urxvt-extensions(1) and there is one that does
what the OP was asking for:


urxvt -pe selection-to-clipboard

selection-to-clipboard - copy the selection to the clipboard each time
a selection is made
Javier
2022-05-26 03:40:44 UTC
Permalink
Post by Ben Collver
I will keep an eye out for something that can work system-wide.
clipnotify looks very close to what you need.

https://github.com/cdown/clipnotify/blob/master/clipnotify.c

The only problem is that it notifies both changes in clipboard and
primary without telling which selection was actually changed.

Just comment this line in the source:

/* XFixesSelectSelectionInput(disp, root, clip, XFixesSetSelectionOwnerNotifyMask); */

and call it primarynotify.c

Compile it and run in the shell

while primarynotify; do
xclip -o -selection primary | xclip -i -selection clipboard
done

NOTE: I have not tested this.
Ben Collver
2022-06-20 22:19:46 UTC
Permalink
Post by Javier
clipnotify looks very close to what you need.
https://github.com/cdown/clipnotify/blob/master/clipnotify.c
Hi Javier,

Your example worked as posted. :-)

I changed clipnotify.c as shown below [1]. Then i ran the script below.
This accomplishes what i was wishing for: bidirectional synchronization.
When the primary selection is made, it is copied to the clipboard. When
the clipboard is filled, it is copied to the primary selection.

while :
do
./clipnotify && \
(echo p2c; xclip -o -selection primary |\
xclip -i -selection clipboard) || \
(echo c2p; xclip -o -selection clipboard |\
xclip -i -selection primary)
done

Thanks!
-Ben

[1]
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>
#include <stdio.h>
#include <stdlib.h>

#define EXIT_PRIMARY 0
#define EXIT_CLIPBOARD 1

int main(void) {
Display *disp;
Window root;
Atom clip;
Atom selection;
XEvent evt;

disp = XOpenDisplay(NULL);
if (!disp) {
fprintf(stderr, "Can't open X display\n");
exit(1);
}

root = DefaultRootWindow(disp);

clip = XInternAtom(disp, "CLIPBOARD", False);

XFixesSelectSelectionInput(disp, root, XA_PRIMARY,
XFixesSetSelectionOwnerNotifyMask);
XFixesSelectSelectionInput(disp, root, clip,
XFixesSetSelectionOwnerNotifyMask);

XNextEvent(disp, &evt);
selection = ((XFixesSelectionNotifyEvent *)&evt)->selection;
XCloseDisplay(disp);
if (selection == XA_PRIMARY) {
exit(EXIT_PRIMARY);
} else {
exit(EXIT_CLIPBOARD);
}
}
Javier
2022-06-26 14:53:44 UTC
Permalink
Post by Ben Collver
Post by Javier
clipnotify looks very close to what you need.
https://github.com/cdown/clipnotify/blob/master/clipnotify.c
Hi Javier,
Your example worked as posted. :-)
I changed clipnotify.c as shown below [1]. Then i ran the script below.
This accomplishes what i was wishing for: bidirectional synchronization.
When the primary selection is made, it is copied to the clipboard. When
the clipboard is filled, it is copied to the primary selection.
Congrats for making it work bidirectionally.

BTW you can also implement the same thing in wish (windowing shell,
which is part of TCL) and is interpreted, so you save the compilation step.
Have a look at this:

https://github.com/apenwarr/xclipsync
Ben Collver
2022-06-26 15:10:25 UTC
Permalink
Post by Javier
Congrats for making it work bidirectionally.
BTW you can also implement the same thing in wish (windowing shell...
https://github.com/apenwarr/xclipsync
Nice find, thanks!

Javier
2022-05-26 03:00:27 UTC
Permalink
Post by Ben Collver
* Open rxvt
* Run: autocutsel -debug -verbose -selection PRIMARY
* Open leafpad
* Type text into leafpad: Howdy
* Press Ctrl-A to select the text
* Press Ctrl-X to cut the text
* Open another rxvt
* Run: uname -a
* Double-click to select the word Linux
* In leafpad, click Edit, Paste
It pastes: Howdy
I expected leafpad to paste "Linux".
When I run "cutsel cut" it outputs "Linux".
When I run "xclip -out -selection primary" it outputs "Linux".
When I run "cutsel sel" it outputs "Howdy".
When I run "xclip -out -selection clipboard" it outputs "Howdy".
bash-5.1$ autocutsel -debug -verbose -selection PRIMARY
autocutsel v0.10.1
Selection changed: "Howdy"
New value saved: "Howdy"
sel -> cut: "Howdy"
Updating buffer
Selection changed: "Linux"
New value saved: "Linux"
sel -> cut: "Linux"
Updating buffer
The second-to-the-last line of output led me to expect that
autocutsel copied the word Linux from the selection into the
clipboard, but on the contrary, the clipboard still contains the word
Howdy.
The problem is that autocutsel/cutsel act on the cutbuffers which is
yet another different thing to primary/clipboard/secondary. In the
olden times of X-windows they were the old-fashioned way to exchange
text between selections before the primary selection existed.

https://www.nongnu.org/autocutsel/
https://manpages.debian.org/testing/autocutsel/autocutsel.1.en.html
Loading...