Tmux: sharing current window title (#T) from within nested tmux sessions

command linepromptterminaltmux

I have begun using tmux recently to really improve my terminal sessions. One thing I need to be able to do is connect to a remote server, which is also running tmux. However, I have found that the current window title (denoted as #T in tmux) is not updated while within a secondary tmux shell. I would like to update the primary-tmux-session value from within the secondary session.

"#T" clearly takes on the value processed by PROMPT_COMMAND, which is a command run every time the prompt is reloaded. However, if I connect to a remote server, it opens a server-side-shell, which then opens tmux and this runs a completely new shell within the secondary-tmux session. The only way I know how to update the primary tmux window title is to reload the server-side-shell prompt, but this does not happen from within a secondary tmux session.

Is there any command to connect the two sessions so that they share #T ? I originally thought it might be doable with the terminal-overrides option, but it either does not work or I have set it up wrong.

Any thoughts or suggestions would be greatly appreciated. Thank you!

(PS. I suspect this question is better suited to superuser, but I had previously asked a question on stack-overflow. I hope this is the right forum for this sort of question)

Best Answer

I did some digging and found the answer for myself. Well, the way I have found to accomplish my gaol is not elegant, but it will work to get the job done.

First, you need to have the "set-titles" option turned on in your .tmux.conf file. As well as the appropriate title string:

set -g set-titles on
set -g set-titles-string '#T'

However, if you implement this, you will immediately notice that it doesn't work. The problem is that this only sends the title string up stream if it starts in an xterm variant (tmux/screen TERM variable is "screen*"). So, when starting a nested tmux session, you must fool the terminal by resetting the variable. The following example will keep the TERM suffix (e.g. "-256color").

TEMP_TERM=$TERM
TEMP_TERM_SUFFIX=${TERM#$(echo $TERM | cut -f 1 -d'-')}
TERM="xterm${TEMP_TERM_SUFFIX}"

I am not sure if it matters, but I think it is smart to reset TERM back after closing tmux (so save it into a temp variable). Using this, a simple shell script can be made to open a nested tmux session that sends the #T title variable upstream to it's parent sessions.

This all works, but is somewhat of a pain considering that work must be done to ensure infinite nesting loops aren't created in the event that we automatically start our shell up in tmux. If anyone has a better solution I would love to here it as well!

Related Question