starfry
starfry

Reputation: 9943

how to show current command in tmux pane title

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.

Upvotes: 5

Views: 10916

Answers (3)

nazikus
nazikus

Reputation: 529

As an alternative you could use this one-liner function to prefix your commands in tmux:

panewrap () { printf "\033]2;%s\033\\" "$1"; "$@"; }

it will set the pane title to launche command ($1 - command name) and pass on it execution to terminal ($@ - command name and all of its parameters).

The inconvenience is that you have to prefix an additional word before any command, but I do it occasionally, only when command is intended to run for quite a while (e.g., tail).

Upvotes: 2

Steven Lu
Steven Lu

Reputation: 43417

I'm not sure if you can set it as the title of a pane if it isn't already (It looks like on my tmux 1.8 it already states the command as the title of the pane), but there is the undocumented #{pane_current_command} variable that you may use in your status bar string that will contain the command.

Upvotes: 3

starfry
starfry

Reputation: 9943

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.

Upvotes: 6

Related Questions