Simulating GNU Screen's shelltitle

31 views
Skip to first unread message

krac...@gmx.com

unread,
Dec 1, 2020, 11:49:03 AM12/1/20
to tmux-...@googlegroups.com
Greetings,

For my preferences, tmux's automatic rename is not convenient. If for
example I execute Ranger, a python file manager, I see 'python' in the
label instead of 'ranger'. Also, the name change is not immediate, a
refresh is needed, starting and quiting.

My tmux settings:

set -g automatic-rename on
set -g automatic-rename-format "#{pane_current_command}"

So I'm trying to simulate GNU Screen's shelltitle feature in tmux, where
the command entered in shell (for example ranger) is shown, not the app
running (ie python). It's accurate and immediate, no need to wait for
refresh. It also returns to 'bash' immediately, after quitting app.

In tmux manual I saw that it is possible to be done screen's way:
set -g allow-rename on

and changing bash prompt. I already have this in .bashrc for screen:
case "$TERM" in
screen*|tmux*)
PS1="\n\e[7m\t \e[1m\w \e[m\n\n"'\[\033k\033\\\]''\$ '

I also tried,
PROMPT_COMMAND='printf "\033k\033\134"'

as in tmux manual
(the exact is PROMPT_COMMAND="printf '\033k\033\\';"${PROMPT_COMMAND} )

The result I get is just an asterisk (*) which doesn't change.

Any ideas how to achieve shelltitle's behaviour?
tmux version 2.8 on Debian

Thank you in advance

Nicholas Marriott

unread,
Dec 1, 2020, 1:36:43 PM12/1/20
to krac...@gmx.com, tmux-users
You are renaming it to an empty name, you need to put something in between the \033k and the \033\\.


--
You received this message because you are subscribed to the Google Groups "tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmux-users+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/tmux-users/d114db31-3962-9c0c-b809-1696e813d14f%40gmx.com.

Sven Guckes

unread,
Dec 1, 2020, 1:51:42 PM12/1/20
to tmux-users
* Nicholas Marriott <nicholas...@gmail.com> [2020-12-01 19:42]:
> You are renaming it to an empty name, you need to
> put something in between the \033k and the \033\\.

oh, i'll bite! what would *you* put there? :)

my workaround for ranger: set a given title.

tmux neww -n FILES ranger bin

yeah, i know.. just one workaround for one program. ;)

Sven

> <krac...@gmx.com>:
> > I already have this in .bashrc for screen:
> > case "$TERM" in screen*|tmux*)
> > PS1="\n\e[7m\t \e[1m\w \e[m\n\n"'\[\033k\033\\\]''\$ '
> > I also tried: PROMPT_COMMAND='printf "\033k\033\134"'

krac...@gmx.com

unread,
Dec 1, 2020, 2:00:17 PM12/1/20
to Nicholas Marriott, tmux-users
I need the something in between to be the command I typed in shell.

The configuration in rc file of GNU Screen is:
shelltitle "$ |bash"

so whatever follows $ sign (whatever typed before hitting enter) is
copied in between \033k and the \033\\ and the desired label is set.
Else you get 'bash'. I think that's how it works, not certain, but it works.


Στις 1/12/20 8:36 μ.μ., ο/η Nicholas Marriott έγραψε:
> <mailto:tmux-users%2Bunsu...@googlegroups.com>.
> <https://groups.google.com/d/msgid/tmux-users/d114db31-3962-9c0c-b809-1696e813d14f%40gmx.com>.
>

Nicholas Marriott

unread,
Dec 1, 2020, 2:01:27 PM12/1/20
to krac...@gmx.com, tmux-users
tmux only supports renaming the window with \033k, what you rename it to is up to you.

Nicholas Marriott

unread,
Dec 1, 2020, 2:09:18 PM12/1/20
to krac...@gmx.com, tmux-users
Maybe bash has a way to get the command and print the sequence after you press Enter?


Nicholas Marriott

unread,
Dec 1, 2020, 3:24:32 PM12/1/20
to krac...@gmx.com, tmux-users
I think you want bash's "preexec" hook, although I am not sure exactly how to use it. That is how iTerm2 sends its "before command" escape sequences and whatnot.

As far as doing it in tmux goes I don't really know how screen's implementation works but I don't want to encode special stuff for bash, zsh etc in tmux so I'm thinking of a couple of possibilities.

Perhaps an option to say "when you see X escape sequence, match the rest of this line against this regex and use the first submatch".

Or it might be more flexible just to store the rest of the line in a variable rather than actually renaming the window, then you could put it in window-status-format or status-left or automatic-rename-format or anything else with whatever transformations you like.

But if you can do it with the existing \033k then it hardly seems necessary.



On Tue, 1 Dec 2020, 20:05 , <krac...@gmx.com> wrote:
Well Nicholas, I thought I should wait, perhaps someone would have a
better ideas. Then I saw in github who you are! So nothing more to expect :)

I'll search for a solution through bash, if I find anything I'll post
it. May I make a humble request for this feature, GNU Screen's
shelltitle like behaviour to be added some time in the future? It's
immediate and precise (for my tastes at least).

Thanx for the feedback and the nice app.


Στις 1/12/20 9:09 μ.μ., ο/η Nicholas Marriott έγραψε:

> Maybe bash has a way to get the command and print the sequence after you
> press Enter?
>
>
>
> On Tue, 1 Dec 2020, 19:01 Nicholas Marriott,
> <nicholas...@gmail.com <mailto:nicholas...@gmail.com>> wrote:
>
>     tmux only supports renaming the window with \033k, what you rename
>     it to is up to you.
>
>
>
>     On Tue, 1 Dec 2020, 18:56 , <krac...@gmx.com
>     <mailto:krac...@gmx.com>> wrote:
>
>         I need the something in between to be the command I typed in shell.
>
>         The configuration in rc file of GNU Screen is:
>         shelltitle "$ |bash"
>
>         so whatever follows $ sign (whatever typed before hitting enter) is
>         copied in between \033k and the \033\\ and the desired label is set.
>         Else you get 'bash'. I think that's how it works, not certain,
>         but it works.
>
>
>         Στις 1/12/20 8:36 μ.μ., ο/η Nicholas Marriott έγραψε:
>          > You are renaming it to an empty name, you need to put
>         something in
>          > between the \033k and the \033\\.
>          >
>          >
>          > On Tue, 1 Dec 2020 at 16:49, <krac...@gmx.com
>         <mailto:krac...@gmx.com>
>          >     <mailto:tmux-users%2Bunsu...@googlegroups.com
>         <mailto:tmux-users%252Buns...@googlegroups.com>>.

krac...@gmx.com

unread,
Dec 1, 2020, 3:29:20 PM12/1/20
to Nicholas Marriott, tmux-users
Well Nicholas, I thought I should wait, perhaps someone would have a
better ideas. Then I saw in github who you are! So nothing more to expect :)

I'll search for a solution through bash, if I find anything I'll post
it. May I make a humble request for this feature, GNU Screen's
shelltitle like behaviour to be added some time in the future? It's
immediate and precise (for my tastes at least).

Thanx for the feedback and the nice app.


Στις 1/12/20 9:09 μ.μ., ο/η Nicholas Marriott έγραψε:
> Maybe bash has a way to get the command and print the sequence after you
> press Enter?
>
>
>
> On Tue, 1 Dec 2020, 19:01 Nicholas Marriott,
> <nicholas...@gmail.com <mailto:nicholas...@gmail.com>> wrote:
>
> tmux only supports renaming the window with \033k, what you rename
> it to is up to you.
>
>
>
> On Tue, 1 Dec 2020, 18:56 , <krac...@gmx.com
> <mailto:krac...@gmx.com>> wrote:
>
> I need the something in between to be the command I typed in shell.
>
> The configuration in rc file of GNU Screen is:
> shelltitle "$ |bash"
>
> so whatever follows $ sign (whatever typed before hitting enter) is
> copied in between \033k and the \033\\ and the desired label is set.
> Else you get 'bash'. I think that's how it works, not certain,
> but it works.
>
>
> Στις 1/12/20 8:36 μ.μ., ο/η Nicholas Marriott έγραψε:
> > You are renaming it to an empty name, you need to put
> something in
> > between the \033k and the \033\\.
> >
> >
> > On Tue, 1 Dec 2020 at 16:49, <krac...@gmx.com
> <mailto:krac...@gmx.com>
> >     <mailto:tmux-users%2Bunsu...@googlegroups.com
> <mailto:tmux-users%252Buns...@googlegroups.com>>.

krac...@gmx.com

unread,
Dec 2, 2020, 12:01:45 PM12/2/20
to Nicholas Marriott, tmux-users
Following your advice on preexec hook, I found a solution with help from
stackoverflow community. It's exactly what I needed, accurate, immediate
and probably elegant - depends on bash's hook elegance actually. I post
it here also in case somebody else looks for it:

Added 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\\'"

PS0, changes label to executed command, taking the string from history.
So for example, not only 'vi' but 'vi edited.file'. 'ranger', not 'python'.
PROMPT_COMMAND, returns label to BASH upon returing to shell. Actually
you can put whatever you like, something more fancy like '>_'

On tmux.conf:

set -g allow-rename on

--


Nicholas Marriott:

krac...@gmx.com

unread,
Dec 2, 2020, 12:41:05 PM12/2/20
to tmux-users
Just in case something is broken in my mailist list answer
in line breaking,
I noticed there must be a space between brackets after sed [ ],
I'm sending the link where I posted the solution:

https://unix.stackexchange.com/questions/81220/simulating-gnu-screens-shelltitle-in-tmux/622584#622584
Reply all
Reply to author
Forward
0 new messages