How to use pbcopy and pbpaste

Jorge Yau
2 min readOct 31, 2019

If you’re here reading this, then you’re probably stumped as to why you can’t use crtl-c or ctrl-v to copy and paste code into your terminal. You could use your mouse. But that’s not sleek.

Here’s the thing, the ctrl character when combined with other keys is used to send signals to your machine kernel. Think of them as override commands. Well, you can override your override keys by pressing the ctrl and shift keys at the same time. So:

To copy you, would press ctrl+shift+c
To paste you, would press ctrl+shift+v

Using pbcopy and pbpaste

You can go do the exact same thing in your Mac terminal with pbcopy and pbpaste. I use them everyday. Make it a habit and you will supercharge your productivity.

pbcopy will allow you to copy the output of a command right into your clipboard. Vice-versa for pbpaste — it will allow you to paste your clipboard right into your terminal.

# This will copy all the content within a file.
$ cat myfile.txt | pbcopy
# This will output the contents of your clipboard.
$ pbpaste

Copy and pasting for Linux

Unfortunately the pbcopy and pbpaste commands are exclusively for Mac, but you can recreate these commands on Linux by using xsel. Add these lines inside your .bashrc file and start a new terminal.

# Linux version of OSX pbcopy and pbpaste.
alias pbcopy=’xsel — clipboard — input’
alias pbpaste=’xsel — clipboard — output’

Now you will be able to perform the same commands in the Linux terminal.

# This will copy all the content within a file.
$ cat myfile.txt | pbcopy
# This will output the contents of your clipboard.
$ pbpaste

--

--

Jorge Yau

Senior Web Engineer at Stash. I write about NYC, tech, and the immensity of life.