Skip to content

Instantly share code, notes, and snippets.

@jeebak
Last active August 29, 2015 14:05
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 jeebak/b864027c7b0338832e76 to your computer and use it in GitHub Desktop.
Save jeebak/b864027c7b0338832e76 to your computer and use it in GitHub Desktop.
Wrapper script for pb{copy,paste}, xsel, or text file buffer
#!/bin/bash
#
# https://gist.github.com/jeebak/b864027c7b0338832e76
#
# Wrapper for pb{copy,paste}, as symlinks to this script, that's be in your
# $PATH. i.e.,
# ln -s pasteboard.bash pbcopy
# ln -s pasteboard.bash pbpaste
# The purpose of this script is to provide the familiar pb{copy,paste} commands
# on a Mac, and in Linux, either in X (where $DISPLAY'd be set) or in a
# console/terminal session.
# Remove matching prefix pattern. $(basename $0)
CALLED_AS="${0##*/}"
# Use: head -1, for the case where there'd be multiple matches:
# brew install reattach-to-user-namespace
WHICH="$(which -a "$CALLED_AS" | grep -v "$0" | head -1)"
[[ -z "$WHICH" ]] && WHICH="$(which xsel)"
# Set a text file to use as a buffer, if xsel's not available, or it is and
# $DISPLAY's not set.
if [[ -z "$WHICH" ]] || { [[ "${WHICH##*/}" = "xsel" ]] && [[ -z "$DISPLAY" ]]; }; then
pasteboard_file="${XDG_CACHE_HOME:-$HOME/.cache}"
[[ ! -d "$pasteboard_file" ]] && mkdir -p "$pasteboard_file"
pasteboard_file="$pasteboard_file/pasteboard.txt"
[[ ! -f "$pasteboard_file" ]] && touch "$pasteboard_file"
fi
options=""
case $CALLED_AS in
pbcopy)
[[ ! -z "$pasteboard_file" ]] && exec cat > "$pasteboard_file"
options='--clipboard --input'
;;
pbpaste)
[[ ! -z "$pasteboard_file" ]] && exec cat "$pasteboard_file"
options='--clipboard --output'
;;
esac
exec "$WHICH" $options "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment