automatic-rename show conditionally either current directory or command

539 views
Skip to first unread message

Dr. Johannes Zellner

unread,
Mar 9, 2016, 8:33:16 AM3/9/16
to tmux-...@googlegroups.com
Hi,

I'd like to have automatic-rename behave like this:

1. show path of current dirctory (e.g. #pane_current_path) if the shell
prompt is shown (e.g. if #pane_current_command == bash)
2. show #pane_current_command, if a command different from the shell is
active (if #pane_current_command != bash)

Any ideas how to do that?

--
Johannes

Thomas Adam

unread,
Mar 9, 2016, 8:51:30 AM3/9/16
to Dr. Johannes Zellner, tmux-...@googlegroups.com
Hi,

You could use PROMPT_COMMAND for this (in the case for bash), and for
other shells, something similar to that. Then just send through the
appropriate escape sequences to change the title.

-- Thomas Adam
> --
> 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 post to this group, send an email to tmux-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dr. Johannes Zellner

unread,
Mar 9, 2016, 9:41:34 AM3/9/16
to tmux-...@googlegroups.com

Hi Thomas,

thanks.
As far as I understand, PROMPT_COMMAND can set the tmux window title if
the prompt is drawn -- in this case, PROMPT_COMMAND would then set the
window title to the path of the current directory for example.
But if I'd then start a program in the shell (e.g. vim) PROMPT_COMMAND
would not be triggered and therefore the tmux window title wouln't be
set to the current command (e.g. vim).

Speaking in tmux options I'd need something conditional like

tmux set-option -gw automatic-rename-format "#{? #{pane_current_command}
== bash,#{pane_current_path},#{pane_current_command}}"

but something like the conditional syntax "? #{pane_current_command} ==
bash" is apparently not available in tmux.

btw.

The root of the problem seems to be that setting the window title
manually or from a shell prompt resets automatic-rename to off.
If this wouldn't be the case, I could indeed simply have a bash prompt
setting the window title to the path of the current directory.

--
Johannes

Nicholas Marriott

unread,
Mar 9, 2016, 12:11:15 PM3/9/16
to Dr. Johannes Zellner, tmux-users

You can do conditionals by using substitution, I don't remember the syntax but if you s/bash// it will be empty so false.

Dr. Johannes Zellner

unread,
Mar 9, 2016, 3:48:55 PM3/9/16
to tmux-users

Nicholas,

thanks. Unfortunately I didn't find what the correct syntax of the substitution could be.

Any pointers?

-- 
Johannes

Nicholas Marriott

unread,
Mar 16, 2016, 6:24:22 AM3/16/16
to Dr. Johannes Zellner, tmux-users
man tmux
...
A limit may be placed on the length of the resultant string by prefixing
it by an `=', a number and a colon. Positive numbers count from the
start of the string and negative from the end, so `#{=5:pane_title}' will
include at most the first 5 characters of the pane title, or
`#{=-5:pane_title}' the last 5 characters. Prefixing a time variable
with `t:' will convert it to a string, so if `#{window_activity}' gives
`1445765102', `#{t:window_activity}' gives `Sun Oct 25 09:25:02 2015'.
The `b:' and `d:' prefixes are basename(3) and dirname(3) of the variable
respectively. A prefix of the form `s/foo/bar/:' will substitute `foo'
with `bar' throughout.
...


On Wed, Mar 09, 2016 at 09:48:53PM +0100, Dr. Johannes Zellner wrote:
> Nicholas,
>
> thanks. Unfortunately I didn't find what the correct syntax of the
> substitution could be.
>
> Any pointers?
>
> --
> Johannes
>
> Am 2016-03-09 18:11, schrieb Nicholas Marriott:
>
> You can do conditionals by using substitution, I don't remember the
> syntax but if you s/bash// it will be empty so false.
>
> On 9 Mar 2016 3:41 p.m., "Dr. Johannes Zellner"
> <[2]joha...@zellner.org> wrote:
>
> Hi,
>
> I'd like to have automatic-rename behave like this:
>
> 1. show path of current dirctory (e.g. #pane_current_path) if the
> shell
> prompt is shown (e.g. if #pane_current_command == bash)
> 2. show #pane_current_command, if a command different from the
> shell is
> active (if #pane_current_command != bash)
>
> Any ideas how to do that?
>
> --
> Johannes
>
> --
> 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 [3]tmux-users+...@googlegroups.com.
> To post to this group, send an email to
> [4]tmux-...@googlegroups.com.
> For more options, visit [5]https://groups.google.com/d/optout.
>
> --
> 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 [6]tmux-users+...@googlegroups.com.
> To post to this group, send an email to
> [7]tmux-...@googlegroups.com.
> For more options, visit [8]https://groups.google.com/d/optout.
>
> --
> 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 [9]tmux-users+...@googlegroups.com.
> To post to this group, send email to [10]tmux-...@googlegroups.com.
> For more options, visit [11]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. mailto:joha...@zellner.org
> 2. mailto:joha...@zellner.org
> 3. mailto:tmux-users%2Bunsu...@googlegroups.com
> 4. mailto:tmux-...@googlegroups.com
> 5. https://groups.google.com/d/optout
> 6. mailto:tmux-users%2Bunsu...@googlegroups.com
> 7. mailto:tmux-...@googlegroups.com
> 8. https://groups.google.com/d/optout
> 9. mailto:tmux-users+...@googlegroups.com
> 10. mailto:tmux-...@googlegroups.com
> 11. https://groups.google.com/d/optout

Thomas Sattler

unread,
Mar 16, 2016, 7:34:00 AM3/16/16
to tmux-...@googlegroups.com, Dr. Johannes Zellner
To have this working in "current stable" tmux-2.1 use this:

#(test "#{pane_current_command}" == "bash" &&
echo "#{pane_current_path}" ||
echo "#{pane_current_command}")

or even

#(test "#{pane_current_command}" == "bash" &&
echo "#{pane_current_command}:#{pane_current_path}" ||
echo "#{pane_current_command}")


Personally I don't like it as it makes window titles to long.

Thomas

pallaswept

unread,
Oct 6, 2023, 12:41:39 PM10/6/23
to tmux-users
7 years too late I found my way here. I'm sure I won't be the only one finding this thread in their search engine, so I'll leave the answer. This works for current tmux as of 2023.

set-option -g automatic-rename-format '#{?#{==:#{pane_current_command},bash},#{=-20:pane_current_path},#{pane_current_command}}'

If the command is bash (we're at the prompt) then use the last 20 characters of the path (so "window titles too long" isn't a problem), otherwise use the command we're running.
I decided I'd like to see a little of the path with the command name (command names tend to be fairly short, so there's room, so I'm actually using

set-option -g automatic-rename-format '#{?#{==:#{pane_current_command},bash},#{=-20:pane_current_path},#{=-7:pane_current_path}> #{pane_current_command}}'

Hope this helps.
Reply all
Reply to author
Forward
0 new messages