Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sync X11 primary selection and secondary clipboard

368 views
Skip to first unread message

Ben Collver

unread,
May 7, 2022, 11:29:37 AM5/7/22
to
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

unread,
May 11, 2022, 1:06:35 PM5/11/22
to
On 5/7/22 9:29 AM, Ben Collver wrote:
> Hi,

Hi,

> 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.

> 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

unread,
May 22, 2022, 8:22:22 PM5/22/22
to
On 2022-05-11, Grant Taylor <gta...@tnetconsulting.net> wrote:
> 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

unread,
May 23, 2022, 10:16:30 PM5/23/22
to
Ben Collver <benco...@tilde.pink> wrote:
> 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

unread,
May 24, 2022, 11:36:30 AM5/24/22
to
On 2022-05-24, Javier <inv...@invalid.invalid> wrote:
> 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

unread,
May 25, 2022, 11:00:35 PM5/25/22
to
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


Javier

unread,
May 25, 2022, 11:20:09 PM5/25/22
to
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

unread,
May 25, 2022, 11:40:51 PM5/25/22
to
Ben Collver <benco...@tilde.pink> wrote:
> 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

unread,
Jun 20, 2022, 6:19:48 PM6/20/22
to
On 2022-05-26, Javier <inv...@invalid.invalid> wrote:
> Ben Collver <benco...@tilde.pink> wrote:
> 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

unread,
Jun 26, 2022, 10:53:51 AM6/26/22
to
Ben Collver <benco...@tilde.pink> wrote:
> On 2022-05-26, Javier <inv...@invalid.invalid> wrote:
>> Ben Collver <benco...@tilde.pink> wrote:
>> 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

unread,
Jun 26, 2022, 11:10:27 AM6/26/22
to
On 2022-06-26, Javier <inv...@invalid.invalid> wrote:
> 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!
0 new messages