rename tmux window name to prompt_command, ps1 or remote ssh hostname?

tmux rename-window -t${TMUX_PANE} "Title Text"

This is the proper way to set tmux titles in a window. The $TMUX_PANE variable is set by tmux and is used to differentiate between different panes.


Just for people who came here by searching how to change the title of a tmux session:

Ctrl + B, $

This will give you a prompt, where you can rename the active session.

To change the active window use komma instead:

Ctrl + B, ,

see: How do I rename a session in tmux?


I'm not aware of any way to make it look at your PS1 directly.

However, tmux understands the same commands to set the window name as screen does.

So you can define a function like this in your ~/.bashrc or ~/.zshrc:

settitle() {
    printf "\033k$1\033\\"
}

and then call settitle from anywhere.

For example, you could include it in your PS1 variable, e.g.

PS1='$HOST:$PWD$(settitle $HOST:$PWD)$ '

or via PROMPT_COMMAND:

PROMPT_COMMAND='$(settitle $HOST:$PWD)'
# and don't change PS1

Now I understand you have tmux running on your desktop, and you want ssh commands to have the hostname rather than ssh, that's much easier.

Given you've added settitle to your local ~/.bashrc, all you want to do is add this too:

ssh() {
    settitle "$*"
    command ssh "$@"
    settitle "bash"
}

Replace bash with zsh, or something else more appropriate if necessary.

Tags:

Tmux