Noob(ish) to Linux here, and trying to pick up new/useful tricks; I was wondering if there's some way to send the output of a command straight to the clipboard?

Something similar to (pseudo-code):

dircolors --print-database > clipboard


I will suggest here to use xclip :

1- You will need first to configure X11 display :

vi /etc/ssh/sshd_config   
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no

Restart the sshd service

service sshd restart 

Open new ssh session with X11 forwarding enabled :

ssh -X <your_server>

2- Install libXmu-devel :

yum install libXmu-devel


sudo apt-get install libxmu-dev

3- Clone xclip repository and compile the program :

git clone https://github.com/astrand/xclip.git
make install

4- Add the command as an alias to your profile to create a shortcut that will save a lot of your time :

vi ~/.bashrc
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'

5- Use case :

cat file | pbcopy

Then the content of your file is copied to your clipboard


When I asked I didn't realize there'd be any real difference between doing what I was looking to do when in WSL vs. "regular" (non-wsl) linux.

After looking over a bunch of references, in my WSL case, the easiest way seems to be to use window's clip.exe, rather than installing something like xclip, creating a bunch of aliases/scripts, etc.

I will try those as well on my Raspberry PI (Raspbian), but for now in WSL just doing this is working great:

<command> | clip.exe


dircolors --print-database | clip.exe

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