[TMUX][ZSH] Label ssh panes with remote title or host alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian IsingandClaude Opus 5.5 committed 2026-10-19 11:14:48 +02:00
1 parent 93400d68ab
commit 0db22a9f0c
2 files changed
+59

No files matched your search

+50
View File
@@ -108,3 +108,53 @@ pastefinish() {
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste)
# Mirror the ssh destination into a per-pane tmux option; .tmux.conf labels ssh
# panes (window name, pane border only in windows with ssh panes) by title.
_ssh_host_tmux() {
local -a set=(set -p -t $TMUX_PANE @ssh_host "$1")
[[ -n $1 ]] || set=(set -pu -t $TMUX_PANE @ssh_host)
tmux $set \; if -F -t $TMUX_PANE '#{P:#{@ssh_host}}' \
"set -w -t $TMUX_PANE pane-border-status top" \
"set -wu -t $TMUX_PANE pane-border-status" 2>/dev/null
}
_ssh_host_preexec() {
[[ -n $TMUX ]] || return
local -a w=(${(z)3}) # $2 is size-limited
while [[ $w[1] == *=* || $w[1] == (command|exec|noglob|nocorrect) ]]; do shift w; done
[[ ${w[1]:t} == ssh ]] || return
shift w
local host opt
while (( $#w )); do
case ${(Q)w[1]} in
--) host=${(Q)w[2]}; break ;;
-?*)
# option cluster; stop at the first flag that takes an argument
opt=${(Q)w[1]#-}; shift w
while [[ -n $opt ]]; do
if [[ $opt[1] == [BbcDEeFIiJLlmOoPpRSWw] ]]; then
[[ -n ${opt:1} ]] || shift w
break
fi
opt=${opt:1}
done ;;
*) host=${(Q)w[1]}; break ;;
esac
done
[[ $host == ssh://* ]] && host=${${host#ssh://}%:<->}
host=${host##*@}
[[ -n $host ]] || return
_ssh_host_tmux $host
_ssh_host_set=1
# pane title starts as the alias; a remote that sets its own title wins.
# Runs after OMZ's preexec, so this replaces its "ssh ..." title.
print -rn -- $'\e]2;'"$host"$'\e\\'
}
_ssh_host_precmd() {
(( ${_ssh_host_set:-0} )) || return
_ssh_host_tmux ''
_ssh_host_set=0
}
autoload -Uz add-zsh-hook
add-zsh-hook preexec _ssh_host_preexec
add-zsh-hook precmd _ssh_host_precmd