2

My use-case is that I want that whenever I copy something to CLIPBOARD, it is also saved in PRIMARY. It's mostly assumed that to copy something you need to select it so most of the time this is not needed.

However, sometimes I just click the classic "copy to clipboard" button and get something to CLIPBOARD that it's not in PRIMARY. I use Shift+Insert a lot for pasting and having to track which selection I'm using makes me confused.

I know there are a tools like clipit or parcellite that do something like this, but I wan't something without a GUI, something like a simple systemd service I can launch and forget.

I tried using a systemd service for autocutsel configured like

ExecStartPre=autocutsel -f ExecStart=autocutsel -f --selection PRIMARY 

However this also synchronizes PRIMARY -> CLIPBOARD, which breaks some very usual workflow like selecting text and then replacing it with the contents of the clipboard.

I've looked for this option in the manpage of autocutsel, but I find it kinda confusing, with a lot of mentions to cutbuffer (which I don't think it's used anymore) and Windows which I don't use. So I don't even know if this is possible with autocutsel.

2 Answers 2

4

Here's a quick Python program to do so, using the PyGObject bindings for GTK. I'm not an expert in this, so this is just an example that works for me, using rpm pygobject2 on an old Fedora release. You will have to find the equivalent packages yourself.

#!/usr/bin/python3 # copy clipboard to primary every time it changes # https://unix.stackexchange.com/a/660344/119298 import signal, gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk, Gdk # callback with args (Gtk.Clipboard, Gdk.EventOwnerChange) def onchange(cb, event): text = clipboard.wait_for_text() # convert contents to text in utf8 primary.set_text(text, -1) # -1 to auto set length signal.signal(signal.SIGINT, signal.SIG_DFL) # allow ^C to kill primary = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY) clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) clipboard.connect('owner-change', onchange) # ask for events Gtk.main() # loop forever 
1
  • Python is not really lightweight in my book, but your solution is simple and short enough Commented Aug 1, 2021 at 15:41
0

I made this functionality using a simple bash script, you can run it as a service or any other way you like. It's not ideal as it does not observe for changes and pools. No ideal, but works.

while true; do if [[ "$PRIMARY_TO_CLIP" != "$(xsel -bo)" ]]; then xsel -bo | xsel -pi; PRIMARY_TO_CLIP=$(xsel -bo); fi; sleep 0.5; done 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.