pbcopy & pbpaste for WSL

I use pbcopy and pbpaste on MacOS and Linux quite a bit. It’s a convenient way to pipe the output of something in the terminal to your clipboard or from your clipboard into the terminal. If you’re running WSL (the Windows Subsystem for Linux) you haven’t been able to use this, until now.

Environment

Launch WSL terminal

mkdir ~/bin

Modify your ~/.bashrc or ~/.zshrc depending on which shell you use (I use zshell) and add to the end of the file:

export PATH=$HOME/bin:$PATH

pbcopy

nano ~/bin/pbcopy

paste the following contents:

#!/bin/bash
# pbcopy for wsl
tee <&0 | clip.exe
exit 0

then we need to set permissions and make it excutable

chmod u+x ~/bin/pbcopy

pbpaste

nano ~/bin/pbpaste

paste the following contents:

#!/bin/bash
# pbpaste for WSL
powershell.exe Get-Clipboard | sed 's/\r$//' | sed -z '$ s/\n$//'
exit 0

Now you can use pbpaste and pbcopy just as you would on Linux and MacOS!

3 thoughts on “pbcopy & pbpaste for WSL

Leave a Reply

Your email address will not be published. Required fields are marked *