tmux: clipboard integration

- copy-mode-vi: map  to copy-pipe-and-cancel -> ~/.config/tmux/scripts/copy_to_clipboard.sh
- add C-S-v and M-V to paste from system clipboard (works over SSH)
- add scripts: paste_from_clipboard.sh, copy_to_clipboard.sh

Only staged clipboard-related changes; other edits remain unstaged.
This commit is contained in:
David Chen 2025-09-23 16:41:23 -07:00
parent 449fbc261e
commit 1a8e4cf560
3 changed files with 72 additions and 1 deletions

View file

@ -147,6 +147,17 @@ bind i split-window -h -c "#{pane_current_path}"
bind -n M-f resize-pane -Z
# paste from system clipboard
bind -n C-S-v run -b "~/.config/tmux/scripts/paste_from_clipboard.sh"
bind -T copy-mode-vi C-S-v send-keys -X cancel \; run -b "~/.config/tmux/scripts/paste_from_clipboard.sh"
bind -T copy-mode C-S-v send-keys -X cancel \; run -b "~/.config/tmux/scripts/paste_from_clipboard.sh"
# Alt+Shift+V paste
bind -n M-V run -b "~/.config/tmux/scripts/paste_from_clipboard.sh"
bind -T copy-mode-vi M-V send-keys -X cancel \; run -b "~/.config/tmux/scripts/paste_from_clipboard.sh"
bind -T copy-mode M-V send-keys -X cancel \; run -b "~/.config/tmux/scripts/paste_from_clipboard.sh"
# pane navigation
bind 1 select-pane -t:.1
bind 2 select-pane -t:.2
@ -195,7 +206,7 @@ bind -T copy-mode-vi E send-keys -N 5 -X cursor-down
bind -T copy-mode-vi N send-keys -X start-of-line
bind -T copy-mode-vi I send-keys -X end-of-line
bind -T copy-mode-vi Y send-keys -X copy-end-of-line
bind -T copy-mode-vi y send-keys -X copy-selection-and-cancel
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "~/.config/tmux/scripts/copy_to_clipboard.sh"
bind -T copy-mode-vi = send-keys -X search-again
bind -T copy-mode-vi = send-keys -X search-reverse

View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euo pipefail
content=$(cat | tr -d '\r')
# Update tmux buffer and system clipboard (uses tmux set-clipboard setting)
tmux set-buffer -w -- "$content"
# Best-effort: also call platform clip utilities when available
if command -v pbcopy >/dev/null 2>&1; then
printf '%s' "$content" | pbcopy || true
elif command -v wl-copy >/dev/null 2>&1; then
printf '%s' "$content" | wl-copy --type text || wl-copy || true
elif command -v xclip >/dev/null 2>&1; then
printf '%s' "$content" | xclip -selection clipboard || true
elif command -v xsel >/dev/null 2>&1; then
printf '%s' "$content" | xsel --clipboard --input || true
elif command -v powershell.exe >/dev/null 2>&1; then
powershell.exe -NoProfile -Command Set-Clipboard -Value @"
${content}
"@ || true
fi

View file

@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -euo pipefail
read_clipboard() {
if command -v pbpaste >/dev/null 2>&1; then
pbpaste
return 0
fi
if command -v wl-paste >/dev/null 2>&1; then
wl-paste --no-newline 2>/dev/null || wl-paste
return 0
fi
if command -v xclip >/dev/null 2>&1; then
xclip -selection clipboard -o 2>/dev/null || true
return 0
fi
if command -v xsel >/dev/null 2>&1; then
xsel -o --clipboard 2>/dev/null || true
return 0
fi
if command -v powershell.exe >/dev/null 2>&1; then
powershell.exe -NoProfile -Command Get-Clipboard 2>/dev/null || true
return 0
fi
return 1
}
content=$(read_clipboard || true)
if [[ -z "${content:-}" ]]; then
exit 0
fi
# normalize CRLF -> LF
content=$(printf '%s' "$content" | tr -d '\r')
tmux set-buffer -- "$content"
tmux paste-buffer -p -d