bash 如何在状态栏中显示 tmux 环境变量(作为窗口格式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21138186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:15:35  来源:igfitidea点击:

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

bashstatusbartmux

提问by gospes

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:

并使用状态栏中的输出,使每个窗口状态都是唯一的。我试图通过 2 个场景使其更清楚:



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:

我正在尝试在状态栏中打印一个唯一的每个窗口变量。我在 tmux 中打开了一个 shell,并分别使用以下命令在本地(每个会话)和全局(对于所有会话)存储了 tmux 环境变量:

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:

例如,我可以通过转到另一个 shell(在同一会话中)并键入以下内容来验证这些值:

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:

最初它只将窗口显示为“[]”,此后我将 stderr 重定向添加到 stdout 并得到状态栏显示以下内容:

[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:

PS:这不是状态栏刷新问题,因为我在设置变量以手动强制刷新状态栏后使用了以下命令:

tmux refresh-client -S


Scenario 2

场景二

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

我编写了一个名为“tmuxscript”的小型 bash 脚本,其中仅包含:

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:

我更新了我的 PATH 变量,并将窗口格式更改为“[#(tmuxscript arg1 #I)]”。输出看起来像:

[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) :

时间更新得很好。在脚本中打印窗口索引似乎不起作用。数字 1 表示当前聚焦窗口的窗口索引。这个值是为所有窗口设置的,这不是我想要的。我至少希望看到(注意窗口索引号):

[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?

此外,#I 没有跳过脚本,但文本 'arg1' 是。如何将 tmux 变量传递给脚本?



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:

当活动(聚焦)窗口索引为 1 时,这给了我以下信息:

[globalvalue1] [globalvalue1] [...

Any help is appreciated!

任何帮助表示赞赏!

采纳答案by gospes

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 中指定窗口状态时,您必须使用 -g(全局)标志。

.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:

关键是在创建窗口后,将状态设为本地。此外,每个窗口都需要唯一可识别。幸运的是,这可以通过 tmux 变量“window_id”来完成。一个小脚本,如下所示,将输出一个唯一的窗口变量,这个 id 作为它的第一个参数:

~/tmuxstatus:

〜/ tmuxstatus

#!/bin/bash

VARIABLE="W_"
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.

可能只有 TMUX 解决方案可以将状态设为本地,但我目前没有时间。我在环境变量 PROMPT_COMMAND 的帮助下使用 bash 执行此操作,该变量在显示提示之前进行评估。

.bashrc:

.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:

当改变 W_id 的值时,窗口状态也会改变。看起来像:

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

enjoy!

请享用!

回答by Dean Householder

Perhaps using the echo command you can manually set the title using this syntax:

也许使用 echo 命令您可以使用以下语法手动设置标题:

echo -ne "\033]0;$(tmux show-environment TMUX_STATUS_1)\007"

echo -ne "\033]0;$(tmux show-environment TMUX_STATUS_1)\007"

Try running that in different TMUX windows and see if it changes the title.

尝试在不同的 TMUX 窗口中运行它,看看它是否会更改标题。