5

I'm trying to simulate GNU Screen's shelltitle feature in tmux. I have the following in my ~/.bashrc:

if [[ "$TERM" == screen* ]]; then
    SCREEN_ESC='\[\ek\e\\\]'
else
    SCREEN_ESC=''
fi

PS1="\n\w ${SCREEN_ESC}$ "

and the following in my ~/.screenrc:

shelltitle        "$ |bash"

Setting shelltitle makes GNU Screen search for a newline in the shell and strip away all the characters from $PS1 upto the escape character, and replace it with the first word found after that. This is more clearly explained in the man page:

Here's how it works: you must modify your shell prompt to output a null title-escape-sequence (<esc>k<esc>) as a part of your prompt. The last part of your prompt must be the same as the string you specified for the search portion of the title. Once this is set up, screen will use the title-escape-sequence to clear the previous command name and get ready for the next command. Then, when a newline is received from the shell, a search is made for the end of the prompt. If found, it will grab the first word after the matched string and use it as the command name. If the command name begins with either '!', '%', or '^' screen will use the first word on the following line (if found) in preference to the just- found name.

This is different from tmux's set-option -g set-titles on which only captures the process name. So a Python script script.py would be titled as script.py in Screen (which is the way I want it) whereas, it would be titled as python in tmux (which I don't want.)

I know that one can rename tmux windows by:

printf '\033kWINDOW_NAME\033\\'

So I was wondering if there is some way I can alternatively let tmux know that I've entered a new command. If there is some way I can get the last command entered, I could use Bash's PROMPT_COMMAND to do this. Any ideas?

0

1 Answer 1

1

Alas, after years, a solution! I also had the same problem with tmux and missed screen's behaviour. In a nutshell:

Add this to .bashrc

if [ -v "TMUX" ]; then
    PS0='$(printf "\033k%s\033" "$(HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//")")'
    PROMPT_COMMAND="printf '\033kBASH\033\\'"

and this on tmux.conf

set -g allow-rename on

For an explanation of bashrc, PS0 line have a look at this: Pass every command executed in the bash shell to a variable

For PROMPT_COMMAND I've used BASH as a label when returning to bash, you can put whatever you like (ie >_). Or you can omit PROMPT_COMMAND and leave the last command executed to be seen on the label.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .