
Want your Tmux window titles to automatically show the hostname you connect to via SSH — and restore the local name after you disconnect? This guide will walk you through setting that up step by step.
1. Tmux configuration – ~/.tmux.conf
In your Tmux configuration file, set the default shell command to a custom Bash init file (.bash_tmux
) that will handle window title updates.
1 |
set-option -g default-command 'bash --rcfile ~/.bash_tmux' |
2. Bash init script – ~/.bash_tmux
This file runs at shell startup inside Tmux. It sets the window name to your local hostname and overrides the ssh
command to change the window title dynamically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#!/bin/bash # ~/.bash_tmux # Custom Bash init file for Tmux sessions with dynamic tab naming # Override the default ssh command to dynamically rename the Tmux window ssh() { if [ -n "$TMUX" ]; then # Loop through arguments to find the target host (first non-flag argument) for arg in "$@"; do if [[ "$arg" != -* ]]; then if [[ "$arg" == *@* ]]; then host="${arg#*@}" # extract host from user@host else host="$arg" fi break fi done # Strip any trailing junk or whitespace host="$(echo "$host" | cut -d' ' -f1)" # Rename the Tmux window to the SSH target [ -n "$host" ] && tmux rename-window "$host" # Save the local hostname to restore later local_host="$(hostname -s)" # Run the actual SSH command command ssh "$@" # Restore the original window name after logout tmux rename-window "$local_host" else # Not in Tmux — just run ssh normally command ssh "$@" fi } # Load the user's regular bash configuration source ~/.bashrc # On shell start, if in Tmux, set the window name to the current hostname if [ -n "$TMUX" ]; then tmux rename-window "$(hostname -s)" fi |
3. Reload Tmux config without restarting
To apply the changes without restarting the Tmux session, run the following:
1 |
tmux source-file ~/.tmux.conf |
To manually reload the current shell with the new .bash_tmux
configuration:
1 |
exec bash --rcfile ~/.bash_tmux |
4. Final result
- New Tmux windows automatically show the local hostname
- When connecting via SSH, the window is renamed to the remote host
- When disconnecting, the original local name is restored
Clean, readable, and perfect for sysadmins, devops engineers, and Tmux lovers who appreciate automatic order in their terminal tabs 💪