Pipe a File to Clipboard Using xclip
Here's how to copy the contents of a file to the system clipboard using xclip.
XClip
xclip
is a cli for copying things. Things is X. X Windows. A window management system that you're probably using in Linux.
The MacOS equivalent is pbcopy
.
Piping
This is probably your second-favorite form of piping behind cake piping. Unix pipes take data from the output of one program and "pipe" or transfer it into the input of the next program. The syntax uses a vertical |
, like:
./program_a | ./program_b
Showing File Contents
To show the contents of a file, run the cat
program, (eg, cat myfile.txt
). The output will appear in stdout, in terminal output.
Let's pipe it somewhere else instead. Where? To the clipboard!
Clipboards
But which one?
XClip more precisely deals with selections. The text that is selected is call the primary clipboard, or XA_PRIMARY. The system clipboard is XA_CLIPBOARD.
When we use xclip
, we'll define which selection we want our data to pipe into using the -sel
flag. To specify the system clipboard, we can use "clipboard" or the shortcut, "clip".
The final command, then, is:
cat myfile.txt | xclip -sel clip
Just text and pipes. Unix magic.