-
-
Save nevesnunes/951f77085116a9beea62c9610dda31e8 to your computer and use it in GitHub Desktop.
ssh() { | |
# grep -w: match command names such as "tmux-2.1" or "tmux: server" | |
if ps -p $$ -o ppid= \ | |
| xargs -i ps -p {} -o comm= \ | |
| grep -qw tmux; then | |
# Note: Options without parameter were hardcoded, | |
# in order to distinguish an option's parameter from the destination. | |
# | |
# s/[[:space:]]*\(\( | spaces before options | |
# \(-[46AaCfGgKkMNnqsTtVvXxYy]\)\| | option without parameter | |
# \(-[^[:space:]]* | option | |
# \([[:space:]]\+[^[:space:]]*\)\?\)\) | parameter | |
# [[:space:]]*\)* | spaces between options | |
# [[:space:]]\+ | spaces before destination | |
# \([^-][^[:space:]]*\) | destination | |
# .* | command | |
# /\6/ | replace with destination | |
tmux rename-window "$(echo "$@" \ | |
| sed 's/[[:space:]]*\(\(\(-[46AaCfGgKkMNnqsTtVvXxYy]\)\|\(-[^[:space:]]*\([[:space:]]\+[^[:space:]]*\)\?\)\)[[:space:]]*\)*[[:space:]]\+\([^-][^[:space:]]*\).*/\6/')" | |
command ssh "$@" | |
tmux set-window-option automatic-rename "on" 1> /dev/null | |
else | |
command ssh "$@" | |
fi | |
} |
Is there a way to get the window title to switch when switching panes? or to switch back when i exit the ssh session?
Hi @johnmmcgee
I rewrote the function from above with some help from AI, so that title automatically changes when exiting the ssh session.
# Change tmux window/pane when using ssh so that it shows user@host
ssh() {
if [ -n "${TMUX:-}" ]; then
# Extract the destination host from the command arguments
local dest_user_host=$(
printf "%s " "$@" | awk 'match($0, /[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+/) {print substr($0,RSTART,RLENGTH)}'
)
# Rename tmux window and pane if possible
(
set +e # Continue even if rename fails
tmux display-message -p "#{pane_id}" > /dev/null && tmux rename-window "$dest_user_host"
tmux rename-pane "$dest_user_host" 2>/dev/null || true
) &> /dev/null
command ssh "$@"
# Restore automatic renaming (if enabled)
tmux set-window-option automatic-rename on > /dev/null 2>&1 || true
else
command ssh "$@"
fi
}
Basically I wrote the awk
part and the automatic-rename
part. The rest is AI hehe...
For some reason I couldn't make the AI to write those lines ๐
@Goku-San hmm this seems to no longer work at all. when using yours.
@Goku-San hmm this seems to no longer work at all. when using yours.
The command is ssh -i your_pem_file.pem user@host
and is should work. You put this function in .zshrc
or .bashrc
.
The important part is user@host
- e.g [email protected]
I made it for this format. If you check at the awk
part, specifically the regex, it looks for user@host
.
It doesn't matter if it has other arguments or not...
ah yes i see that now and explains why it did not work. I was able to rework the regex and now its working for how i use it. thank you.
Glad you made it work... Cheers...
Thanks, exactly what I was looking for.... ๐