Last active
August 29, 2015 14:05
-
-
Save jeebak/b864027c7b0338832e76 to your computer and use it in GitHub Desktop.
Wrapper script for pb{copy,paste}, xsel, or text file buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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