15

When copying text to the clipboard, xclip provides several selection targets:

 -selection
    specify which X selection to use, options are:
    "primary" to use XA_PRIMARY (default), 
    "secondary" for XA_SECONDARY 
    "clipboard" for XA_CLIPBOARD

Is there a way to target multiple selections?

I have tried the following options

  1. echo "Hello world" | xclip -i -selection primary -selection clipboard
  2. echo "Hello world" | xclip -i selection primary | xclip -i selection clipboard
  3. echo "Hello world" | xclip -i selection primary,clipboard

but none of them worked.

2 Answers2

22

I have tried the following options

echo "Hello world" | xclip -i selection primary | xclip -i selection clipboard  

You were really close there...
If you use -f with the first xclip command it will print the text back to stdout and you can pipe it to the second xclip command:

echo "Hello World" | xclip -i -sel p -f | xclip -i -sel c

From man xclip:

-f, -filter
            when xclip is invoked in the in mode with output level set to
            silent (the defaults), the filter option will cause xclip to print
            the text piped to standard in back to standard out unmodified
don_crissti
  • 82,805
  • Thanks! I am accepting this solution because: 1) It informs about an interesting feature of xclip. 2) It allows me to pipe xclip commands which is quite convenient and 3) For some odd reason, it's the only answer so far that gives a working solution in my experiments for binding copy-pipe in tmux (this is what prompted this question) – Amelio Vazquez-Reina Mar 25 '13 at 20:18
  • 2
    If your distro provides xsel, an alternative to xclip, you can use echo 'Hello World' | xsel -i -p && xsel -o -p | xsel -i -b. It's slightly different and less efficient than xclip due to xsel lacking a filter option. But if you're stuck with xsel then this is a way to solve the problem. – starfry Feb 05 '17 at 11:55
5

I don't use xclip, so there may be a way to do this natively which I'm not aware of. In any case, this should work assuming your shell is bash:

echo "Hello world" | tee >(xclip -i -selection primary) >(xclip -i -selection clipboard) >/dev/null

>() is a form of process substitution. bash replaces each with the path to a file descriptor which is connected to the standard input of the program within the parentheses.

Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • Thanks! Do you know if that syntax compatible with zsh too? – Amelio Vazquez-Reina Mar 25 '13 at 17:33
  • @user815423426 As far as I know, yes. – Chris Down Mar 25 '13 at 19:38
  • I have tested here on my zsh and it works like a charm! – SergioAraujo Aug 22 '19 at 11:09
  • I've tested this on Bash on Ubuntu and openSUSE Tumbleweed. Both seem to have a problem specifically with xclip in this context. The first time it runs, only one of the selections will be correctly set (usually the secondary for me, but my understanding is that it may be non-deterministic). The other will get the previous content on the other selection. The second time it is run, since the contents are updated on the secondary, the primary is also set correctly. – NotTheDr01ds Oct 13 '21 at 21:13