You are not logged in.

#1 2025-03-09 11:40:02

funeshroom
Member
Registered: 2025-02-17
Posts: 18

[SOLVED] Which clipboard managers can preserve PRIMARY selection?

I really like how PRIMARY selection works on Xorg: select text to copy, middle click to paste.
The only issue I have is that PRIMARY selection contents do not survive termination of the application that it is copied from, because app's internal memory is used for storing the selection.

Are there clipboard managers that can alleviate this nuisance and get PRIMARY selections to survive their app's lifetime, and which if yes?
I already use autocutsel for synchronizing PRIMARY and CLIPBOARD selections but this preserves the first one only as CLIPBOARD selection i.e. I can't use middle click to paste which is really neat.
So what autocutsel does is not the solution I'm looking for.

Last edited by funeshroom (2025-03-11 19:38:38)

Offline

#2 2025-03-09 17:02:57

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 73,781

Re: [SOLVED] Which clipboard managers can preserve PRIMARY selection?

"xsel -k"?

Offline

#3 2025-03-09 19:07:33

funeshroom
Member
Registered: 2025-02-17
Posts: 18

Re: [SOLVED] Which clipboard managers can preserve PRIMARY selection?

seth wrote:

"xsel -k"?

That does what I want but it needs to be run manually every time.
Do you (or anyone else) know a good way to automate it without just making it be run constantly?
Ideally, it would be run whenever the X app that is currently associated with PRIMARY selection is about to be closed; a good alternative is also making it run whenever any X app is about to be closed.

Offline

#4 2025-03-09 20:23:42

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 73,781

Re: [SOLVED] Which clipboard managers can preserve PRIMARY selection?

That does what I want but it needs to be run manually every time.

"xsel -k" does not fork into background and stay there as basic clipboard manager all the time?

Running "constantly" is the nature of every clipboard manager, "X app …is about to be closed" would technically be "about to disconnect from the server" and the "about to" part is privvy to that very app.

Offline

#5 2025-03-10 13:12:32

funeshroom
Member
Registered: 2025-02-17
Posts: 18

Re: [SOLVED] Which clipboard managers can preserve PRIMARY selection?

seth wrote:

That does what I want but it needs to be run manually every time.

"xsel -k" does not fork into background and stay there as basic clipboard manager all the time?

Running "constantly" is the nature of every clipboard manager, "X app …is about to be closed" would technically be "about to disconnect from the server" and the "about to" part is privvy to that very app.

xsel -k does not run constantly, it's a one-time command.
As for the "must be running constantly" part, if that's just how things are I'm alright with that, but in that case I would prefer to have a separate compiled tool that constantly does what xsel -k does instead of having it be constantly run in a script: that's a lot lighter on system resources.

Offline

#6 2025-03-10 16:40:51

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 73,781

Re: [SOLVED] Which clipboard managers can preserve PRIMARY selection?

Well, see https://wiki.archlinux.org/title/Clipboard#Managers
Whether any of those will be "a lot lighter on system resources" than looping

while true; do xsel -nk; done

would be any interesting question.
clipsim looked promising, but depends on xclip - what means it's likely spawning that…

Offline

#7 2025-03-11 19:34:55

funeshroom
Member
Registered: 2025-02-17
Posts: 18

Re: [SOLVED] Which clipboard managers can preserve PRIMARY selection?

seth wrote:

Well, see https://wiki.archlinux.org/title/Clipboard#Managers
Whether any of those will be "a lot lighter on system resources" than looping

while true; do xsel -nk; done

would be any interesting question.
clipsim looked promising, but depends on xclip - what means it's likely spawning that…

I've come up with this script:

#!/bin/sh

while xsel -nk; do
    [ -z "$(xsel -o)$(xsel -so)" ] && sleep 0.5
done

I think it's clear what it does.
It's not ideal but sleep here is necessary because xsel -nk will exit immediately with 0 if both PRIMARY and SECONDARY selections are empty (as they always are on startup) and in that case without it the script consumes around 10% of CPU on my machine which is unacceptable.
I know I could instead fill one of them (SECONDARY is a better choice because it's almost never used) with some sort of an "invisible character" but this is still a better option: some apps may react unexpectedly when I accidentally paste that "invisible character" into them, and also because if I ever (accidentally or intentionally) were to clear PRIMARY and SECONDARY selections the script would return to consuming around 10% of my CPU, potentially causing damage to an ongoing work.

Perhaps the ideal solution would be to modify xsel itself and give it an option to wait even when the selections are empty, but I have no time for that now.

Since I'm satisfied with this solution I'm marking this thread as SOLVED.

Edit: Also, xsel -nk will exit with 1 immediately when the X server instance is stopped (so normally when the user logs out), and there is no reason for the script to continue running when that happens, which is why its exit code is the condition of the loop.

Last edited by funeshroom (2025-03-11 19:38:18)

Offline

Board footer

Powered by FluxBB