Bash – how to show current command in tmux pane title

bashtmux

I would like to update the tmux pane-title with the current executing command or, if no command, the name of the current shell. What I've come up with so far is this, in bashrc:

case ${TERM} in

  screen*)       
    PROMPT_COMMAND='printf "\033]2;bash\033\\"'
    set -o functrace
    trap 'echo -ne "\033]2;$BASH_COMMAND\033\\"' DEBUG
    ;;

   ...

esac

the method was derived from here:
http://www.davidpashley.com/articles/xterm-titles-with-bash.html

This partially works – it does what is needed but causes other problems: the first prompt in a new shell is prefixed with

"'"' DEBUG"

and all remaining commands with

"

It also prevents some commands given on the command line to fail, for example:

$ ps -h $$
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html

So, while the above does allow the current command to be displayed in the tmux pane title, it does not work. Has anyone else got a better solution to this, or a suggestion as to what is wrong with the above?

Thanks.

Best Answer

Here is one way to have the tmux pane title updated every time you execute a command in BASH. Put code like the below in ~/.bashrc:

case ${TERM} in

    screen*)

        # user command to change default pane title on demand
        function title { TMUX_PANE_TITLE="$*"; }

        # function that performs the title update (invoked as PROMPT_COMMAND)
        function update_title { printf "\033]2;%s\033\\" "${1:-$TMUX_PANE_TITLE}"; }

        # default pane title is the name of the current process (i.e. 'bash')
        TMUX_PANE_TITLE=$(ps -o comm $$ | tail -1)

        # Reset title to the default before displaying the command prompt
        PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'update_title'   

        # Update title before executing a command: set it to the command
        trap 'update_title "$BASH_COMMAND"' DEBUG

        ;;

        ... other cases for different terminals ...

esac

}

The function update_title prints the escape sequence that changes the tmux pane title. It sets the pane title to the default (the value of $TMUX_PANE_TITLE) or to whatever is given as an argument.

The function title is for end-user convenience: it changes the value of the default title in $TMUX_PANE_TITLE. The end user can at any time change the title to whever they want by doing:

$ title my new pane title

The initial title is set to the name of the running shell (i.e. 'bash').

Bash executes anything in $PROMPT_COMMAND prior to displaying a prompt. This is set so that the update_title function gets executed before every prompt to set the prompt to the default title.

The trap causes Bash to execute $BASH_COMMAND before executing any command. It is set so that the update_title function gets executed before every command to set the prompt to the text of that command.

Other notes

  • while working this out, I discovered that set -o functrace or set -T (as described by person linked to in the question) causes RVM to break. The reason for it being suggested was to allow prompts to change in subshells but the lack of this wasn't a problem to me.

  • To get the initial title, I wanted to use the more succinct ps -ho comm $$ but this seemed to not work inside tmux with the above in place. I am not sure why so opted for something else that did work.