Last Updated: December 26, 2018
·
22.95K
· sandersch

pbcopy and pbpaste on Linux

When I started using OS X, I found the pbcopy and pbpaste command line utilities to be indispensable and an integral part of my workflow. These utilities allow me to interact with the system clipboard in a unixy, file-oriented manner that works really well with pipes and redirection, reducing my need to reach for the mouse to copy or paste.

On systems other than OS X, this functionality can be recreated using an X11 tool called xsel which is easily available on most unix systems with X11, including Linux and BSD. I use the following two aliases which behave like the OS X commands:

alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'

I share a common dotfiles set up between systems which includes my bash aliases. Since these aliases are not useful on OS X, I use the following block to add the aliases on non-OS X systems:

# Simulate OSX's pbcopy and pbpaste on other platforms
if [ ! $(uname -s) = "Darwin" ]; then
    alias pbcopy='xsel --clipboard --input'
    alias pbpaste='xsel --clipboard --output'
fi

3 Responses
Add your response

Nice tip! By the way, why not just check for the existence of xsel and the non-existence of pbcopy?

if ! which pbcopy >/dev/null && which xsel >/dev/null; then
  alias ...
end
over 1 year ago ·

@rstacruz I think that the error message given on, for example, Ubuntu is much more useful when the alias is defined, i.e.

The program 'xsel' is currently not installed.  You can install it by typing:
sudo apt-get install xsel

instead of:

No command 'pbcopy' found, did you mean:
  Command 'pcopy' from package 'pcopy' (universe)
  Command 'bcopy' from package 'bacula-sd-pgsql' (main)
  Command 'bcopy' from package 'bacula-sd-sqlite3' (main)
  Command 'bcopy' from package 'bacula-sd-mysql' (main)
pbcopy: command not found
over 1 year ago ·

Thanks! I was on automatic when I typed:

[ ~] 
lsl /var/crash | pbcopy
 No command 'pbcopy' found, did you mean:
 Command 'bcopy' from package 'bacula-sd-pgsql' (main)
 Command 'bcopy' from package 'bacula-sd-mysql' (main) 
 Command 'bcopy' from package 'bacula-sd-sqlite3' (main)
pbcopy: command not found

Then remembered I was Ubuntu not OsX, Google brought me here and it was exactly what I needed.

over 1 year ago ·