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.
By default the active pane’s title is displayed on the right side of the tmux status line (the default global value of the session variable status-right is "#22T" %H:%M %d-%b-%y, which shows 22 characters of the pane’s title, the time, and the date).
tmux does support per-pane titles, but it does not provide a per-pane location to display these titles. You can set a pane’s title with the escape sequence ESC ]2; … ESC \ (e.g. see the section called Names and Titles in the tmux manpage). You could do this from the shell like this: Each pane’s title defaults to the system’s hostname.
Working with tmux commands 1 new-window To create a new window 2 split-window -v To split the window and create a new pane (vertically) 3 split-window -h To split the window and create a new pane (horizontally) 4 select-window -t :=0 To select window from terminal 0 5 next-window To go to the next window 6 last-window To go to the last window
From man tmux: display-panes [-t target-client] (alias: displayp) Display a visible indicator of each pane shown by target-client. See the display-panes-time, display-panes-colour, and display-panes-active-colour session options.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With