From 5c8b0cc0c1782b34790548498018fb9e0300992b Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Mon, 17 Aug 2015 22:53:45 -0400 Subject: [PATCH 1/2] 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. --- lib/clipboard.zsh | 67 ++++++++++++++++++++++++++++ plugins/coffee/coffee.plugin.zsh | 10 ++--- plugins/copydir/copydir.plugin.zsh | 4 +- plugins/copyfile/copyfile.plugin.zsh | 8 ++-- 4 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 lib/clipboard.zsh diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh new file mode 100644 index 00000000..24b7380f --- /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: +# +# | clipcopy - copies stdin to clipboard +# +# clipcopy - 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 diff --git a/plugins/coffee/coffee.plugin.zsh b/plugins/coffee/coffee.plugin.zsh index 1a7bedd8..c7b486a6 100644 --- a/plugins/coffee/coffee.plugin.zsh +++ b/plugins/coffee/coffee.plugin.zsh @@ -6,11 +6,11 @@ cf () { } # compile & copy to clipboard cfc () { - cf $1 | pbcopy + cf $1 | clipcopy } -# compile from pasteboard & print -alias cfp='coffeeMe "$(pbpaste)"' +# compile from clipboard & print +alias cfp='coffeeMe "$(clippaste)"' -# compile from pasteboard and copy to clipboard -alias cfpc='cfp | pbcopy' +# compile from clipboard and copy to clipboard +alias cfpc='cfp | clipcopy' diff --git a/plugins/copydir/copydir.plugin.zsh b/plugins/copydir/copydir.plugin.zsh index 37bb5e08..4b918e81 100644 --- a/plugins/copydir/copydir.plugin.zsh +++ b/plugins/copydir/copydir.plugin.zsh @@ -1,3 +1,5 @@ +# Copies the pathname of the current directory to the system or X Windows clipboard function copydir { - pwd | tr -d "\r\n" | pbcopy + emulate -L zsh + print -n $PWD | clipcopy } \ No newline at end of file diff --git a/plugins/copyfile/copyfile.plugin.zsh b/plugins/copyfile/copyfile.plugin.zsh index 944a903c..f4eca5ac 100644 --- a/plugins/copyfile/copyfile.plugin.zsh +++ b/plugins/copyfile/copyfile.plugin.zsh @@ -1,5 +1,7 @@ +# Copies the contents of a given file to the system or X Windows clipboard +# +# copyfile function copyfile { - [[ "$#" != 1 ]] && return 1 - local file_to_copy=$1 - cat $file_to_copy | pbcopy + emulate -L zsh + clipcopy $1 } From b6d78df62c540245f67ffc900d0b1a17a6dfb77e Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Sun, 4 Oct 2015 03:42:24 -0400 Subject: [PATCH 2/2] clip*: add xsel support --- lib/clipboard.zsh | 45 +++++++++++++++++++++--------- plugins/copydir/copydir.plugin.zsh | 2 +- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 24b7380f..b663800a 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -31,15 +31,21 @@ function clipcopy() { 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 + if which xclip &>/dev/null; then + if [[ -z $file ]]; then + xclip -in -selection clipboard + else + xclip -in -selection clipboard $file + fi + elif which xsel &>/dev/null; then + if [[ -z $file ]]; then + xsel --clipboard --input + else + cat "$file" | xsel --clipboard --input + fi else - xclip -in -selection clipboard $file + print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 + return 1 fi fi } @@ -50,6 +56,17 @@ function clipcopy() { # # clippaste - writes clipboard's contents to stdout # +# clippaste | - pastes contents and pipes it to another process +# +# clippaste > - paste contents to a file +# +# Examples: +# +# # Pipe to another process +# clippaste | grep foo +# +# # Paste to a file +# clippaste > file.txt function clippaste() { emulate -L zsh if [[ $OSTYPE == darwin* ]]; then @@ -57,11 +74,13 @@ function clippaste() { 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 + if which xclip &>/dev/null; then + xclip -out -selection clipboard + elif which xsel &>/dev/null; then + xsel --clipboard --output + else + print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 return 1 fi - xclip -out -selection clipboard fi -} \ No newline at end of file +} diff --git a/plugins/copydir/copydir.plugin.zsh b/plugins/copydir/copydir.plugin.zsh index 4b918e81..c4510624 100644 --- a/plugins/copydir/copydir.plugin.zsh +++ b/plugins/copydir/copydir.plugin.zsh @@ -2,4 +2,4 @@ function copydir { emulate -L zsh print -n $PWD | clipcopy -} \ No newline at end of file +}