Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 48 lines (35 sloc) 1.09 KB
# Copies data to clipboard from stdin.
function clipboard-copy() {
emulate -L zsh
# If there's no display, use the OCS-52 ANSI code to allow the host
# terminal emulator to copy into the host clipboard.
if ! has-display; then
local buf=$( cat "$@" )
# Copy to tmux if possible
is-tmux && tmux set-buffer "$buf"
# Otherwise copy to terminal
local len=$( printf %s "$buf" | wc -c )
local max=74994
test $len -gt $max &&
echo "$0: input is $(( len - max )) bytes too long" >&2
# Base64 encode the data.
esc="\033]52;c;$( printf %s "$buf" | head -c $max | base64 | tr -d '\r\n' )\a"
is-tmux && esc="\033Ptmux;\033$esc\033\\"
printf "$esc"
elif is-darwin; then
elif is-cygwin; then
cat > /dev/clipboard
elif command-exists xclip; then
xclip -in -selection clipboard
elif command-exists xsel; then
xsel --clipboard --input
local message="clipboard-copy: Platform $(uname -s) not supported or "
message+="xclip/xsel not installed"
print message >&2
return 1
clipboard-copy $@