bash 如何在 tmux 窗格标题中显示当前命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14356857/
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 04:15:56  来源:igfitidea点击:

how to show current command in tmux pane title

bashtmux

提问by starfry

I would like to update the tmux pane-title with the current executing command or, if no command, the name of the current shell. What I've come up with so far is this, in bashrc:

我想用当前正在执行的命令更新 tmux 窗格标题,如果没有命令,则使用当前 shell 的名称。到目前为止我想出的是这个,在 bashrc 中:

case ${TERM} in

  screen*)       
    PROMPT_COMMAND='printf "3]2;bash3\"'
    set -o functrace
    trap 'echo -ne "3]2;$BASH_COMMAND3\"' DEBUG
    ;;

   ...

esac

the method was derived from here: http://www.davidpashley.com/articles/xterm-titles-with-bash.html

该方法源自此处:http: //www.davidpashley.com/articles/xterm-titles-with-bash.html

This partially works - it does what is needed but causes other problems: the first prompt in a new shell is prefixed with

这部分有效 - 它完成了所需但会导致其他问题:新 shell 中的第一个提示前缀为

"'"' DEBUG"

and all remaining commands with

以及所有剩余的命令

"

It also prevents some commands given on the command line to fail, for example:

它还可以防止在命令行上给出的某些命令失败,例如:

$ ps -h $$
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html

So, while the above does allow the current command to be displayed in the tmux pane title, it does not work. Has anyone else got a better solution to this, or a suggestion as to what is wrong with the above?

因此,虽然上述确实允许在 tmux 窗格标题中显示当前命令,但它不起作用。有没有其他人对此有更好的解决方案,或者对上述问题有什么建议?

Thanks.

谢谢。

回答by starfry

Here is one way to have the tmux pane title updated every time you execute a command in BASH. Put code like the below in ~/.bashrc:

这是每次在 BASH 中执行命令时更新 tmux 窗格标题的一种方法。将如下代码放入~/.bashrc

case ${TERM} in

    screen*)

        # user command to change default pane title on demand
        function title { TMUX_PANE_TITLE="$*"; }

        # function that performs the title update (invoked as PROMPT_COMMAND)
        function update_title { printf "3]2;%s3\" "${1:-$TMUX_PANE_TITLE}"; }

        # default pane title is the name of the current process (i.e. 'bash')
        TMUX_PANE_TITLE=$(ps -o comm $$ | tail -1)

        # Reset title to the default before displaying the command prompt
        PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'update_title'   

        # Update title before executing a command: set it to the command
        trap 'update_title "$BASH_COMMAND"' DEBUG

        ;;

        ... other cases for different terminals ...

esac

}

}

The function update_titleprints the escape sequence that changes the tmux pane title. It sets the pane title to the default (the value of $TMUX_PANE_TITLE) or to whatever is given as an argument.

该函数update_title打印更改 tmux 窗格标题的转义序列。它将窗格标题设置为默认值( 的值$TMUX_PANE_TITLE)或作为参数给出的任何内容。

The function titleis for end-user convenience: it changes the value of the default title in $TMUX_PANE_TITLE. The end user can at any time change the title to whever they want by doing:

该函数title是为了最终用户的方便:它更改 中默认标题的值$TMUX_PANE_TITLE。最终用户可以随时通过以下方式将标题更改为他们想要的任何位置:

$ title my new pane title

The initial title is set to the name of the running shell (i.e. 'bash').

初始标题设置为正在运行的 shell 的名称(即“bash”)。

Bash executes anything in $PROMPT_COMMANDprior to displaying a prompt. This is set so that the update_titlefunction gets executed before every prompt to set the prompt to the default title.

Bash 在$PROMPT_COMMAND显示提示之前执行任何操作。这被设置为update_title在每个提示之前执行该函数以将提示设置为默认标题。

The trap causes Bash to execute $BASH_COMMANDbefore executing any command. It is set so that the update_titlefunction gets executed before every command to set the prompt to the text of that command.

陷阱导致 Bash$BASH_COMMAND在执行任何命令之前先执行。它被设置为update_title在每个命令之前执行该函数以将提示设置为该命令的文本。

Other notes

其他注意事项

  • while working this out, I discovered that set -o functraceor set -T(as described by person linked to in the question) causes RVM to break. The reason for it being suggested was to allow prompts to change in subshells but the lack of this wasn't a problem to me.

  • To get the initial title, I wanted to use the more succinct ps -ho comm $$but this seemed to not work inside tmux with the above in place. I am not sure why so opted for something else that did work.

  • 在解决这个问题时,我发现set -o functraceor set -T(如问题中链接的人所述)导致 RVM 损坏。建议它的原因是允许在子shell 中更改提示,但缺少这个对我来说不是问题。

  • 为了获得最初的标题,我想使用更简洁的,ps -ho comm $$但这在 tmux 中似乎不起作用,上面的内容就位。我不知道为什么选择其他有效的方法。

回答by Steven Lu

I'm not sure if you can set it as the title of a pane if it isn't already (It looks like on my tmux 1.8 it already states the command as the title of the pane), but there is the undocumented #{pane_current_command}variable that you may use in your status bar string that will contain the command.

我不确定您是否可以将它设置为窗格的标题(如果它还没有的话)(看起来在我的 tmux 1.8 上它已经将命令声明为窗格的标题),但是有一个未记录的#{pane_current_command}变量您可以在包含命令的状态栏字符串中使用。

回答by nazikus

As an alternative you could use this one-liner function to prefix your commands in tmux:

作为替代方案,您可以使用此单行函数在 tmux 中为您的命令添加前缀:

panewrap () { printf "3]2;%s3\" ""; "$@"; }

it will set the pane title to launche command ($1- command name) and pass on it execution to terminal ($@- command name and all of its parameters).

它将窗格标题设置为启动命令($1- 命令名称)并将其执行传递到终端($@- 命令名称及其所有参数)。

The inconvenience is that you have to prefix an additional word before any command, but I do it occasionally, only when command is intended to run for quite a while (e.g., tail).

不便之处在于您必须在任何命令之前添加一个附加词,但我偶尔会这样做,仅当命令打算运行很长时间时(例如,tail)。