1

I'm aware of xsel and xclip, ( also discussed in: https://stackoverflow.com/questions/749544/pipe-to-from-the-clipboard ), and use them often.

However, now I would like to have the primary clipboard dumped to stdout, but in "follow mode". Currently, if I try this:

$ xclip -o
Currently, if I try this:
$ xsel -o
Currently, if I try this:
$

... the commands exit immediately.

What I would like instead, is a behavior similar to tail -f /var/log/syslog - that is, "follow mode": tail blocks the terminal, and whenever a new line shows in the file, it dumps it to standard output.

I'd like something similar, but for whenever I make a new copy (i.e., press Ctrl-C, or Ctrl-Shift-C in terminal) - that is, whenever I change the primary clipboard contents.

Is there a command that will help me do that in bash?

3
  • 1
    I do similar for myself Would you be happy with a 16 line python script? – Tagwint Oct 23 '17 at 18:52
  • Thanks @Tagwint - would be quite happy with that :) – sdaau Oct 23 '17 at 19:18
  • Are you using desktop which supports appindicators by any chance ? – Sergiy Kolodyazhnyy Oct 24 '17 at 17:45
3

OK, here's a python solution. I don't think it needs comments, it does exactly what requested, but let me know if my assumption is wrong.

#!/usr/bin/env python
import sys 
import signal
import gi
gi.require_version("Gtk", "3.0")
from   gi.repository import Gtk, Gdk 

def pcallBack(*args):
    print pclip.wait_for_text() 

if __name__ == '__main__':    
        import signal    
        signal.signal(signal.SIGINT, signal.SIG_DFL)    
        pclip = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)
        pclip.connect('owner-change',pcallBack)
        Gtk.main()
1
  • Awesome, @Tagwint, thanks so much for that! It's just that I needed the dump only when I make a copy (that is, press Ctrl-C), your script dumps every time I make a new text selection - but that is easily fixed by changing Gdk.SELECTION_PRIMARY into Gdk.SELECTION_CLIPBOARD (corrected my OP to reflect that). Thanks again - cheers! – sdaau Oct 24 '17 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.