conf2/zsh/.oh-my-zsh/lib/clipboard.zsh

87 lines
2.1 KiB
Bash
Raw Normal View History

2019-01-12 15:47:23 +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.
2019-01-12 15:47:23 +00:00
# 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|msys)* ]]; then
if [[ -z $file ]]; then
cat > /dev/clipboard
else
cat $file > /dev/clipboard
fi
else
if (( $+commands[xclip] )); then
if [[ -z $file ]]; then
xclip -in -selection clipboard
else
xclip -in -selection clipboard $file
fi
elif (( $+commands[xsel] )); then
if [[ -z $file ]]; then
xsel --clipboard --input
else
cat "$file" | xsel --clipboard --input
fi
else
print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
return 1
fi
fi
}
2019-01-12 15:47:23 +00:00
# clippaste - "Paste" data from clipboard to stdout
#
# Usage:
#
# clippaste - writes clipboard's contents to stdout
#
# 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
function clippaste() {
2019-01-12 15:47:23 +00:00
emulate -L zsh
if [[ $OSTYPE == darwin* ]]; then
pbpaste
elif [[ $OSTYPE == (cygwin|msys)* ]]; then
cat /dev/clipboard
2019-01-12 15:47:23 +00:00
else
if (( $+commands[xclip] )); then
xclip -out -selection clipboard
elif (( $+commands[xsel] )); then
xsel --clipboard --output
else
print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
return 1
fi
2019-01-12 15:47:23 +00:00
fi
}