Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a tmux environment variable in the status bar (as window format)

What it comes down to is, I would like to

  • print a variable unique to the window ,or
  • run a script unique to the window

and use the output in the status bar such that each window status is unique. I've tried to make it more clear through 2 scenarios:


Scenario 1

I'm trying to print a unique per window variable in the status bar. I've opened a shell inside tmux and have stored the tmux environment variable locally (per session) and globally (for all sessions) with the following commands, respectively:

bash> tmux set-environment TMUX_STATUS_1 localvalue1
bash> tmux set-environment -g TMUX_STATUS_1 globalvalue1

I can verify these values for instance by going the another shell (in the same session) and typing:

bash> tmux show-environment TMUX_STATUS_1
    TMUX_STATUS_1=localvalue1
bash> tmux show-environment -g TMUX_STATUS_1
    TMUX_STATUS_1=globalvalue1

I've tried to print the above value in the statusbar (both the local and global value) and have configured the window format as follows:

WINDOW='[#(tmux show-environment -g TMUX_STATUS_#I 2>&1)]'
setw -g window-status-current-format $WINDOW
setw -g window-status-format $WINDOW

Initially it only showed windows as '[]', after this I added the redirection of stderr to stdout and got the status bar showing the following:

[unknown variable: TMUX_STATUS_1] [unknown variable: TMUX_STATUS_2] [...

What needs to be changed to make the statusbar show (according to previous commands):

[globalvalue1] [unknown variable: TMUX_STATUS_2] [...

PS: it's not a status bar refresh issues, as i've used the following command after setting the variable to manually force a refresh of the statusbar:

tmux refresh-client -S

Scenario 2

I've written a small bash script called 'tmuxscript' containing only:

echo "$(date '+%S') window:$(tmux display -p '#I') args:$@"

I updated my PATH variable and I have changed the window format to '[#(tmuxscript arg1 #I)]'. The output looks like:

[47 window:1 args:arg1] [47 window:1 args:arg1] [...

The time updates nicely. Printing the window index inside the script doesn't seem to work. The number 1 represents the window index of the currently focused window. This value is set for all windows, which is not what I want. I would at least expect to see (note the window index number) :

[47 window:1 args:arg1] [47 window:2 args:arg1] [...

Also, #I isn't getting past to the script, but the text 'arg1' is. How can I pas tmux variables to the script?


EDIT: I have now also tried setting the window status to:

'[#(tmux show-environment -g TMUX_STATUS_$\(tmux display -p "#I"\) 2>&1 | sed "s:^.*=::" )]'

Which gives me the following when the active (focused) window index is 1:

[globalvalue1] [globalvalue1] [...

Any help is appreciated!

like image 630
gospes Avatar asked Jan 15 '14 13:01

gospes


1 Answers

Ok, I figured it out.

The problem lies in the fact that you are obligated to use the -g (global) flag when specifying a window-status in .tmux.conf.

.tmux.conf:

WINDOW="[#I #20W]"
set-window -g window-status-current-format $WINDOW
set-window -g window-status-format $WINDOW

The key is to make the status local after creating a window. Also, each window needs to be uniquely identifiable. Luckily this can be done by tmux variable 'window_id'. A small script, shown below, will output a variable unique to the window with this id as its first argument:

~/tmuxstatus:

#!/bin/bash

VARIABLE="W_$1"
VALUE=$(tmux show-environment -g $VARIABLE 2>&1)
VALUE=${VALUE#*=}
echo $VALUE

There is probably a TMUX only solution to make the status local, but I don't currently have the time. I'm using bash to do it with the aid of the environment variable PROMPT_COMMAND, which is evaluated just before the prompt is shown.

.bashrc:

function __prompt_command (){
   if [ -n "$TMUX" ] && [ ! -n "$TMUX_INIT" ]; then
        W=$(tmux display -p '#{window_id}')
        VARIABLE="W_$W"
        VALUE="value_$W"
        STATUS="[#I #(~/tmuxstatus $W)]"
        tmux set-option quiet on;
        tmux set-environment -g $VARIABLE $VALUE;
        tmux set-window window-status-current-format "$STATUS";
        tmux set-window window-status-format "$STATUS";
        export TMUX_INIT="done";
    fi; 
}
export PROMPT_COMMAND=__prompt_command

When changing the value of W_id, the window status changes also. It looks like:

[1 value_@0] [2 value_@1] [3 value_@2] [4 value_@3]

enjoy!

like image 148
gospes Avatar answered Oct 23 '22 18:10

gospes