Last Updated: February 25, 2016
·
290
· niedzielski

Seamlessly Work Across GUIs and Command Line with Copy and Paste

The clipboard provides a very convenient way to exchange data between different programs and is especially convenient for working with GUIs which can't use pipelines. Add the following function to your .zshrc / .bashrc:

cb()
{
  # OS X: pbcopy / pbpaste
  # Cygwin: putclip / getclip
  # Linux: xclip (and others)
  if [[ ! -t 0 ]]
  then
    # Input (copy).
    xclip -sel c
  else
    # Output (paste).
    xclip -sel c -o
  fi
}

Examples:

# Check for the existence of a collection of files.
ls $(cb)

# Copy the webpage at the clipboard URL.
curl $(cb)|cb

# Copy the concatenate of a collection of files.
cb|xargs -rd\\n cat|cb