How to keep terminal window title in sync with tmux window

fishitermterminaltmux

I am using a program called "Timing" to track my time. Timing uses the name of the current window in iTerm for categorization purposes.

I'd like to set the current title of iTerm to match the name of the currently active tmux window.

I've set up a file at ~/.config/fish/functions/fish_title.fish with:

function fish_title
  tmux display-message -p '#W'
end

This successfully outputs the current Tmux window title to the terminal window whenever I open a new terminal window. However, if I rename a tmux window, or switch to a new window, the terminal does not update.

It does update if I use something like pwd in fish_title:

function fish_title
  pwd
end

Any ideas on how to make fish_title grab the tmux window name dynamically?

Best Answer

First thing to point out is that you can do this via tmux completely independent of your shell. Add the following to your ~/.tmux.conf; for example:

set-option -g set-titles on
set-option -g set-titles-string "#S / #W"

Also, you need to ensure iTerm2 is configured to let programs set the title via an escape sequence. See the "Terminal may set tab/window title" section in https://www.iterm2.com/documentation-one-page.html.

But your real problem is that the escape sequence sent by the fish_title function isn't passed through by tmux. Tmux instead uses the value to set the pane title. This is explained, albeit not clearly, in the "NAMES AND TITLES" section of man tmux. Add #T to the set-titles-string to see that it is in fact, being set to the same value as the window name via your fish_title function.

Related Question