Skip to content

Instantly share code, notes, and snippets.

@icylace
Created October 11, 2025 20:55
Show Gist options
  • Save icylace/8f24e034711d5cd0e2f30805e814542a to your computer and use it in GitHub Desktop.
Save icylace/8f24e034711d5cd0e2f30805e814542a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Pipes in/out of the macOS clipboard.
#
# Usage: <command> | cb # Copy stdin.
# cb <file> # Copy a file's contents.
# cb | <command> # Pipe clipboard's content into a command.
# cb > <file> # Paste into a file.
#
# Based on:
# https://stackoverflow.com/a/19458217
# https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/clipboard.zsh
#
cb() {
if [ -p /dev/stdin ] ; then
# Copy from stdin.
pbcopy
return
fi
if [ -z "$1" ] ; then
# Paste to stdout.
pbpaste
return
fi
if [ -f "$1" ] ; then
# Copy a file's contents.
pbcopy < "$1"
return
fi
local red='\e[0;31m'
local reset='\e[0m'
echo "${red}ERROR: $1 is not a file and there's nothing piped in from stdin.${reset}"
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment