Tmux - Solving The Problem Of Window Names

I was lucky enough to get a book on using tmux for Christmas. Following through the examples I noticed that I was not getting the expected behaviour when attempting to set window names.

tmux new-window -n console

What this should do is create a new tmux window and within it a window called “console”. What I was getting was a tmux window containing a window named after my command prompt e.g. ‘bob@computer ~’. This was bothering me so I set about researching the problem, unluckily for me its not one that is that common. By chance I located a post linking to a RedHat bug report The report outlines a problem regarding a change in terminal escape codes and highlighted an identical problem with screen as I am having with tmux. I checked the escape codes in my .bashrc file, nothing wrong there. Next stop was /etc/bashrc.


 1if [ -z "$PROMPT_COMMAND" ]; then
 2  case $TERM in
 3  xterm*|vte*)
 4    if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
 5        PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
 6    elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
 7        PROMPT_COMMAND="__vte_prompt_command"
 8    else
 9        PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
10    fi
11    ;;
12  screen*)
13    if [ -e /etc/sysconfig/bash-prompt-screen ]; then
14        PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
15    else
16        PROMPT_COMMAND='printf "\033k%s@%s:%s\033\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
17    fi
18    ;;
19  *)
20    [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
21    ;;
22  esac

/etc/bashrc contained the above code section. It comprises two different sections for different terminals, each has its own prompt with different escape codes. The but report highlighted that the problem lay with the 033k escape code where as 033]0 worked. I should only be using the problematic escape code if I am using a screen terminal. So which am I using?

echo $TERM

The result from running the above command in my gnome-terminal was “xterm-256color”. This was expected and what I assumed I was using. Running it from inside a tmux session I got “screen-256color”. Tmux using a screen terminal is pointing back to the problematic escape code. Time to go back to the .tmux.conf and check my settings.


1# Use 256 term colours
2set -g default-terminal "screen-256color"

There is my problem line, I have set it to use the screen terminal, oops.

.tmux.conf - Correct

1# Use 256 term colours
2set -g default-terminal "xterm-256color"

By using the xterm-256color terminal I correct the problem with the escape code. Rerunning my initial tmux command tmux opens with a window named ‘console’. Problem solved.