Shell – passing tmux variable into shell-command in tmux.conf

configurationshelltmux

I'm trying to edit my bottom left status section of tmux. I would like it to show

Session: #{session_name}

The current maximum length for this string is set at 20 using this setting

status-left-length 20

This works fine but once I enter a session name that is longer than 20 (including "Session: "), tmux just cuts off the text. I would like process the whole string so that anything longer than 20 characters will show up like this

Session: mysessio…

ie. the string gets trimmed to 20 characters and the last three characters are replaced with dots.
I have a working bash script doing what I would like

string="verylongstringfortesting"
lengthLimit=10

if [ ${#string} -gt $lengthLimit ]
then
  echo ${string:0:$(($lengthLimit-3))}"..."
fi

how do I go about embedding this in tmux.conf?? I know about #(shell-command) and I have tried #(echo #{session_name}) but that doesn't seem to return the session name.

Best Answer

Some information collected to help in search of a solution:

  • Doing #(echo #{session_name}) outputs nothing, but #(echo \"#{session_name}\") shows the session name, which looks promising, but...

  • The reason it works when quoted is that the echo command literally gets the text #{session_name} where, without quotes, the shell considers it a comment, and with the quotes, echos it back verbatim to tmux. Tmux expands the sequence after the command exits

  • This means we cannot manipulate the expanded string within the shell.

Alternative approaches

  • Attempting to set a limit, like #7S to limit the session name to 7 characters always applies the limit but doesn't let you test it, so #7... will always show the ellipsis even when not wanted, so that doesn't work

  • Attempting to access directly via tmux commands doesn't seem to obtain reasonable results. For example, tmux list-sessions -F "#{client_session}" shows nothing, and tmux list-clients -F "#{client_session}" shows the sessions of every client, but if you have more than one client, there isn't an obvious way to tell which one is the one running the command.

I haven't delved into the source yet, but I suspect tmux runs the command prior to even knowing which session it might be for.

Your best bet to get this functionality might be to tweak the source code.

Version 1.8 does the calculation of length in status_redraw_get_left( around line 79 of status.c and the writing of it in status_redraw( at around line 322.

Related Question