[tmux] Allow cross-ssh copying

This commit is contained in:
Fabian Ising
2025-04-18 08:49:54 +02:00
parent edde785a03
commit 0e251ec36a
5 changed files with 74 additions and 2 deletions

View File

@@ -25,3 +25,5 @@ else
rm -rf $HOME/.config/nvim rm -rf $HOME/.config/nvim
ln -Tsfv $PWD/nvim $HOME/.config/nvim ln -Tsfv $PWD/nvim $HOME/.config/nvim
fi fi
sudo ln -Tsfv $PWD/ssh_tty /etc/sudoers.d/ssh_tty

64
scripts/yank Executable file
View File

@@ -0,0 +1,64 @@
#!/bin/sh
#
# Usage: yank [FILE...]
#
# Copies the contents of the given files (or stdin if no files are given) to
# the terminal that runs this program. If this program is run inside tmux(1),
# then it also copies the given contents into tmux's current clipboard buffer.
# If this program is run inside X11, then it also copies to the X11 clipboard.
#
# This is achieved by writing an OSC 52 escape sequence to the said terminal.
# The maximum length of an OSC 52 escape sequence is 100_000 bytes, of which
# 7 bytes are occupied by a "\033]52;c;" header, 1 byte by a "\a" footer, and
# 99_992 bytes by the base64-encoded result of 74_994 bytes of copyable text.
#
# In other words, this program can only copy up to 74_994 bytes of its input.
# However, in such cases, this program tries to bypass the input length limit
# by copying directly to the X11 clipboard if a $DISPLAY server is available;
# otherwise, it emits a warning (on stderr) about the number of bytes dropped.
#
# See http://en.wikipedia.org/wiki/Base64 for the 4*ceil(n/3) length formula.
# See http://sourceforge.net/p/tmux/mailman/message/32221257 for copy limits.
# See http://sourceforge.net/p/tmux/tmux-code/ci/a0295b4c2f6 for DCS in tmux.
#
# Written in 2014 by Suraj N. Kurapati and documented at http://goo.gl/NwYqfW
buf=$( cat "$@" )
# Create the OSC52 escape string.
len=$( printf %s "$buf" | wc -c ) max=74994
test $len -gt $max && echo "$0: input is $(( len - max )) bytes too long" >&2
esc="\033]52;c;$( printf %s "$buf" | head -c $max | base64 | tr -d '\r\n' )\a"
test -n "$TMUX" && esc="\033Ptmux;\033$esc\033\\"
# Output the string to waiting terminals
printf "$esc"
# Attempt to push to the raw SSH_TTY if that exists.
test -n "$SSH_TTY" && printf "$esc" > $SSH_TTY
if [ -n "$TMUX" ]; then
#push the OSC52 esc string to the clients directly.
# tmux_clients=$(tmux list-clients -F "#{client_tty}")
# readarray -t tmux_clients <<<"$tmux_clients"
# for c in ${tmux_clients[@]}; do
# printf "$esc" > $c
# done
#
# And add it directly to the TMUX copy buffer.
test -n "$TMUX" && tmux set-buffer "$buf"
fi
# copy to X11 if possible...
test -n "$DISPLAY" && command -v xsel > /dev/null && command -v xclip > /dev/null \
&& printf %s "$buf" | { xsel -ib || xclip -sel c ;} && exit
# copy to pbcopy on MacOS
command -v pbcopy > /dev/null && printf %s "$buf" | pbcopy
# Copy to remote pbcopy daemon, if attached
if $(nc -z localhost 5556); then
printf %s "$buf" | nc localhost 5556
fi

1
ssh_tty Normal file
View File

@@ -0,0 +1 @@
Defaults env_keep += "SSH_TTY SSH_CONNECTION SSH_CLIENT"

View File

@@ -1,4 +1,4 @@
set-environment -g PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin" set-environment -g PATH "$HOME/dotfiles/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
set -g default-terminal "xterm-256color" set -g default-terminal "xterm-256color"
set -g history-limit 20000 set -g history-limit 20000
@@ -59,6 +59,11 @@ set-window-option -g mode-keys vi
set-option -s set-clipboard on set-option -s set-clipboard on
set -g @yank_with_mouse off set -g @yank_with_mouse off
set -g @yank_action 'copy-pipe' set -g @yank_action 'copy-pipe'
bind-key -Tcopy-mode-vi 'C-right' send -X next-word
bind-key -Tcopy-mode-vi 'C-left' send -X previous-word
unbind-key -T copy-mode-vi MouseDragEnd1Pane
# Allow copy across SSH
set -g @override_copy_command 'yank > #{pane_tty}'
if '[ `uname` == Darwin ]' \ if '[ `uname` == Darwin ]' \
'source-file ~/.tmux/.mac_config' 'source-file ~/.tmux/.mac_config'

View File

@@ -1,4 +1,4 @@
export PATH="$HOME/.local/bin:$PATH" export PATH="$HOME/.local/bin:$HOME/dotfiles/scripts:$PATH"
[[ -z $SSH_AUTH_SOCK ]] || export FORWARD_SOCK=$SSH_AUTH_SOCK [[ -z $SSH_AUTH_SOCK ]] || export FORWARD_SOCK=$SSH_AUTH_SOCK
[[ -f ~/.zsh/.powerline_config ]] && source ~/.zsh/.powerline_config [[ -f ~/.zsh/.powerline_config ]] && source ~/.zsh/.powerline_config