Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
# 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 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 $@