Skip to content

Instantly share code, notes, and snippets.

@erichs
Created July 30, 2015 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erichs/573b15e127bbec235498 to your computer and use it in GitHub Desktop.
Save erichs/573b15e127bbec235498 to your computer and use it in GitHub Desktop.
pb - DWIM clipboard manipulation
pb() {
if [[ -p /dev/stdin ]]; then # STDIN is attached to a pipe
pbcopy
fi
if [[ ! -t 0 && ! -p /dev/stdin ]]; then # STDIN is attached to a redirect
pbcopy
fi
if [[ -t 1 ]]; then # STDOUT is attached to a TTY
if [[ -t 0 ]]; then # STDIN is attached to TTY
pbpaste
fi
fi
if [[ -p /dev/stdout ]]; then # STDOUT is attached to a pipe
pbpaste
fi
if [[ ! -t 1 && ! -p /dev/stdout ]]; then # STDOUT is attached to a redirection
pbpaste
fi
}
@erichs
Copy link
Author

erichs commented Jul 30, 2015

You'll need to source the above to use it as a command:

source ./pb.sh

pb could be used like:

echo text | pb    # copy

or just

pb    # paste

or

echo text | pb | cat -    # copy and paste inline

or

pb </tmp/file >/tmp/file2  # copy from file redirect and paste into file redirect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment