github twitter linkedin email
Automatically Set The Name of tmux Windows
May 15, 2018
2 minutes read

I recently updated to Fedora 28 which includes a updated version of tmux - tmux 2.7. I quickly noted that my window title was no longer being set as before and eventually identified the issue: a breaking change in 2.7. Below is my guide on ensuring tmux displays a useful title for each window.

What I'm looking to achieve is a prompt like the following:

/media/automatically-set-tmux-window-name.png

More specifically, I want $(hostname): $(basename $PWD). tmux provides two ways to do this.

The automatic-rename options

The tmux(1) man page describes the following options:

automatic-rename [on | off]

Control automatic window renaming. When this setting is enabled, tmux will rename the window automatically using the format specified by automatic-rename-format. This flag is automatically disabled for an individual window when a name is specified at creation with new-window or new-session, or later with rename-window, or with a terminal escape sequence. It may be switched off globally with:

set-window-option -g automatic-rename off
automatic-rename-format format
The format (see FORMATS) used when the automatic-rename option is enabled.
status-interval interval
Update the status line every interval seconds. By default, updates will occur every 15 seconds. A setting of zero disables redrawing at interval.

We can use a combination of these to configure the status line by adding the following to our .tmux.conf file:

set-option -g status-interval 1
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'

More detail can be found in a related StackOverflow question.

Unfortunately this is not ideal as there will still be some lag between the window name being set and it being reflected in the UI. This brings us to the alternative.

Terminal escape sequences

tmux can also take advantage of terminal escape sequences. By emitting these from our shell, we can ensure the changes to the window name happen automatically. To this end, bash(1) provides the PROMPT_COMMAND shell variable:

PROMPT_COMMAND
If set, the value is executed as a command prior to issuing each primary prompt.

We can configure this in our .bashrc file like so:

case "$TERM" in
linux|xterm*|rxvt*)
  export PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME%%.*}: ${PWD##*/}\007"'
  ;;
screen*)
  export PROMPT_COMMAND='echo -ne "\033k${HOSTNAME%%.*}: ${PWD##*/}\033\\" '
  ;;
*)
  ;;
esac

Note

This is actually a little more complicated that necessary but it with ensure we get the format requested both when we're using tmux (the latter case) and when we are not (the former).

You also need to configure the following in your .tmux.conf in order to work around the breaking change mentioned above:

set -g allow-rename on

Once done, restart tmux as you'll see your changes propagated.


Back to posts


comments powered by Disqus