Capture Command Line Output to the Linux Clipboard
This blog post explains how to use the xclip command line tool to pipe output from a command line into the Linux clipboard, equivalent to the clip.exe command on Windows. Apparently, there are a few options to do this, but nothing standard in Linux itself, so you may want to research before committing to this approach.
I didn't install xclip - Fedora installed it when I tried to call it the first time. Installation instructions vary by Linux distribution. The installation command for Fedora would be:
sudo dnf install xclip
Next, I would define two aliases and a function, potentially in ~/.bashrc: one alias for consuming to the clipboard, one for pasting from the clipboard, and a function to run a command, consume both stderr and stdout to the clipboard, and also write that content to the output stream.
alias cclip='xclip -selection clipboard'
alias pclip='xclip -selection clipboard -o'
tclip () {
"$@" 2>&1 | tee >(xclip -selection clipboard)
}
Now you can use cclip to pipe command line output into the clipboard and pclip to output the clipboard to the command line.
echo hello | cclip # capture stdout to clipboard
pclip # render from clipboard
To capture stderr as well, add 2>&1 before the pipe (|):
bruh 2>&1 | cclip # capture stdout and stderr to clipboard
pclip # render from clipboard
You can use tclip to run the command, capture its output, and see that output (minus the color coding, unfortunately). But why would anyone want do do that, right? ;-)
tclip cargo build # capture and output stdout and stderr