Zshell send-to-clipboard function

in code

I thought it would be fun to share, because I use a slightly different version of this in my image upload script. The function:

  1. Scans the system via which to find the first installed “send to clipboard” program.
  2. Fall back upon cat if none are found (from there you can copy and paste).
  3. Redirect the input to the command.

I came up with this because I wanted a platform-agnostic clipboard utility without tiresome parsing of uname. Why check for the platform when you can check for the executable?

I picked the go-to choices for Cygwin, Linux and OS X respectively: putclip, xclip and pbcopy.

function to_clipboard {
    local -a CLIPBOARDS
    CLIPBOARDS=('xclip -sel clip' 'pbcopy' 'putclip')

    # Dump the string to STDOUT if an appropriate clipboard program does not exist.
    local FOUND_CLIPBOARD='cat'

    for PROGRAM in $CLIPBOARDS; do
        if [[ $(has_executable $PROGRAM) == 0 ]]; then

    eval $FOUND_CLIPBOARD < << "$1"

Use like:

to_clipboard 'He who controls the spice controls the universe.'

Six Months in the Mountain Kingdom

in me

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