Skip to content

Instantly share code, notes, and snippets.

@seb26
Last active August 23, 2024 22:03
Show Gist options
  • Save seb26/b0ba48f92fcb76c528b47b0cc4ba258f to your computer and use it in GitHub Desktop.
Save seb26/b0ba48f92fcb76c528b47b0cc4ba258f to your computer and use it in GitHub Desktop.
Set the title of a Tmux pane to the SSH remote host currently active in that pane
## Tested on Tmux 3.4, macOS
## Set the title of a pane to
## - if during an SSH session, then `user@remotehost` - which is retrieved from first parameter after `ssh` of the active ssh process in that pane's tty
## - if not, the pane's current path
set-option -g automatic-rename on
set-option -g automatic-rename-format '#(PANE_SSH_REMOTE_HOST=$(ps -o command= -t "$(tmux display-message -p "#{pane_tty}")" | grep ssh | grep -v grep | cut -d" " -f2); [ -z "$PANE_SSH_REMOTE_HOST" ] && PANE_TITLE=$(tmux display-message -p "#{pane_current_path}") || PANE_TITLE="[$PANE_SSH_REMOTE_HOST]"; echo "$PANE_TITLE")'
set-option -g status-interval 1 # seconds
## Same as above, but:
## - if not, the pane's current path with realpaths turned into ~
set-option -g automatic-rename-format '#(PANE_SSH_REMOTE_HOST=$(ps -o command= -t "$(tmux display-message -p "#{pane_tty}")" | grep ssh | grep -v grep | cut -d" " -f2); [ -z "$PANE_SSH_REMOTE_HOST" ] && PANE_CURRENT_PATH=$(tmux display-message -p "#{pane_current_path}") && PANE_TITLE=$(echo "${PANE_CURRENT_PATH/#$HOME/~}") || PANE_TITLE="[$PANE_SSH_REMOTE_HOST]"; echo "$PANE_TITLE")'
## OPTIONS
## For an SSH session, set the pane title to all SSH parameters, not just the first param that is the user@remotehost
## (e.g., if you call `ssh user@remotehost "echo hi; sleep 5"`, returns that full command)
## change:
cut -d" " -f2-
## to:
cut -d" " -f2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment