Search code examples
linuxshellterminalpipexclip

How to copy on xclip and see the command output on the shell?


Whenever I use xclip the command output goes right the clipboard but I also need to see it on the terminal:

I already tried this commands using pipe and & |&:

pwd |& xclip -rmlastnl -selection clipboard

but doesn't work (the output goes right to the clipboard but I don't see it on the terminal).

the expected result is symple, whenever I do this:

pwd | xclip

I should get both the output on the shell:

path/working/directory

and also the same result on my clipboard.


Solution

  • Thanks to the hint of @raminnietzsche I found the solution:

    Since I'm using KDE I needed to send the copy strings to the default clipboard. So I achieved this with this command:

    xclip -rmlastnl -selection clipboard

    Since the copied strings goes to the main clipboard, the same options should be used when we use xclip -o. So the code to achieve what I wanted should be this one:

    pwd | xclip -rmlastnl -selection clipboard | xclip -o -rmlastnl -selection clipboard
    

    The problem is that with pipe the commands seems to run simultaneously, so the paste content (with xclip -o etc...) will be the one already stored and not the new one. In other words it will copy the command output but will paste the last copied content.

    To solve this I used && instead of the second | in order to paste only after the copy happens.

    I also created some alias on my zsh (or bash) config for the sake of time and clarity:

    alias copy="xclip -rmlastnl -selection clipboard"
    alias past="xclip -o -rmlastnl -selection clipboard"
    

    So the working command is:

    pwd | copy && past