diff options
author | Andrew Janke <andrew@apjanke.net> | 2015-08-17 22:53:45 -0400 |
---|---|---|
committer | Andrew Janke <andrew@apjanke.net> | 2015-08-18 03:49:51 -0400 |
commit | 5c8b0cc0c1782b34790548498018fb9e0300992b (patch) | |
tree | fe9f02d629f970470369ef7f2f6938589277db16 /lib | |
parent | 192de6bcffb0294e19f4203f6f7dc1a7f3e427be (diff) | |
download | zsh-5c8b0cc0c1782b34790548498018fb9e0300992b.tar.gz zsh-5c8b0cc0c1782b34790548498018fb9e0300992b.tar.bz2 zsh-5c8b0cc0c1782b34790548498018fb9e0300992b.zip |
Add clipcopy() and clippaste() generic cross-platform CLI clipboard functions.
Change copydir, copyfile, and coffee plugins to use them, instead of the Mac-only `pbcopy` command.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/clipboard.zsh | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh new file mode 100644 index 000000000..24b7380f7 --- /dev/null +++ b/lib/clipboard.zsh @@ -0,0 +1,67 @@ +# 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 + which xclip &>/dev/null + if [[ $? != 0 ]]; then + print "clipcopy: Platform $OSTYPE not supported or xclip not installed" >&2 + return 1 + fi + if [[ -z $file ]]; then + xclip -in -selection clipboard + else + xclip -in -selection clipboard $file + fi + fi +} + +# clippaste - "Paste" data from clipboard to stdout +# +# Usage: +# +# clippaste - writes clipboard's contents to stdout +# +function clippaste() { + emulate -L zsh + if [[ $OSTYPE == darwin* ]]; then + pbpaste + elif [[ $OSTYPE == cygwin* ]]; then + cat /dev/clipboard + else + which xclip &>/dev/null + if [[ $? != 0 ]]; then + print "clipcopy: Platform $OSTYPE not supported or xclip not installed" >&2 + return 1 + fi + xclip -out -selection clipboard + fi +}
\ No newline at end of file |