2015-08-18 02:53:45 +00:00
|
|
|
# System clipboard integration
|
|
|
|
#
|
|
|
|
# This file has support for doing system clipboard copy and paste operations
|
|
|
|
# from the command line in a generic cross-platform fashion.
|
|
|
|
#
|
|
|
|
# On OS X and Windows, the main system clipboard or "pasteboard" is used. On other
|
|
|
|
# Unix-like OSes, this considers the X Windows CLIPBOARD selection to be the
|
|
|
|
# "system clipboard", and the X Windows `xclip` command must be installed.
|
|
|
|
|
|
|
|
# clipcopy - Copy data to clipboard
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# <command> | clipcopy - copies stdin to clipboard
|
|
|
|
#
|
|
|
|
# clipcopy <file> - copies a file's contents to clipboard
|
|
|
|
#
|
|
|
|
function clipcopy() {
|
|
|
|
emulate -L zsh
|
|
|
|
local file=$1
|
|
|
|
if [[ $OSTYPE == darwin* ]]; then
|
|
|
|
if [[ -z $file ]]; then
|
|
|
|
pbcopy
|
|
|
|
else
|
|
|
|
cat $file | pbcopy
|
|
|
|
fi
|
|
|
|
elif [[ $OSTYPE == cygwin* ]]; then
|
|
|
|
if [[ -z $file ]]; then
|
|
|
|
cat > /dev/clipboard
|
|
|
|
else
|
|
|
|
cat $file > /dev/clipboard
|
|
|
|
fi
|
|
|
|
else
|
2016-10-10 21:01:37 +00:00
|
|
|
if (( $+commands[xclip] )); then
|
2015-10-04 07:42:24 +00:00
|
|
|
if [[ -z $file ]]; then
|
|
|
|
xclip -in -selection clipboard
|
|
|
|
else
|
|
|
|
xclip -in -selection clipboard $file
|
|
|
|
fi
|
2016-10-10 21:01:37 +00:00
|
|
|
elif (( $+commands[xsel] )); then
|
2015-10-04 07:42:24 +00:00
|
|
|
if [[ -z $file ]]; then
|
|
|
|
xsel --clipboard --input
|
|
|
|
else
|
|
|
|
cat "$file" | xsel --clipboard --input
|
|
|
|
fi
|
2015-08-18 02:53:45 +00:00
|
|
|
else
|
2015-10-04 07:42:24 +00:00
|
|
|
print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
|
|
|
|
return 1
|
2015-08-18 02:53:45 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# clippaste - "Paste" data from clipboard to stdout
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# clippaste - writes clipboard's contents to stdout
|
|
|
|
#
|
2015-10-04 07:42:24 +00:00
|
|
|
# clippaste | <command> - pastes contents and pipes it to another process
|
|
|
|
#
|
|
|
|
# clippaste > <file> - paste contents to a file
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# # Pipe to another process
|
|
|
|
# clippaste | grep foo
|
|
|
|
#
|
|
|
|
# # Paste to a file
|
|
|
|
# clippaste > file.txt
|
2015-08-18 02:53:45 +00:00
|
|
|
function clippaste() {
|
|
|
|
emulate -L zsh
|
|
|
|
if [[ $OSTYPE == darwin* ]]; then
|
|
|
|
pbpaste
|
|
|
|
elif [[ $OSTYPE == cygwin* ]]; then
|
|
|
|
cat /dev/clipboard
|
|
|
|
else
|
2016-10-10 21:01:37 +00:00
|
|
|
if (( $+commands[xclip] )); then
|
2015-10-04 07:42:24 +00:00
|
|
|
xclip -out -selection clipboard
|
2016-10-10 21:01:37 +00:00
|
|
|
elif (( $+commands[xsel] )); then
|
2015-10-04 07:42:24 +00:00
|
|
|
xsel --clipboard --output
|
|
|
|
else
|
|
|
|
print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
|
2015-08-18 02:53:45 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
2015-10-04 07:42:24 +00:00
|
|
|
}
|