使 gnome-terminal 显示作为标题运行的命令

dai*_*isy 11 gnome-terminal

我希望通过 gnome-terminal 的标题栏可以看到正在运行的命令的名称,例如解压缩,但似乎不可能,如果运行的应用程序没有明确设置标题,即使我选择了“替换初始标题”配置文件对话框中的选项。

小智 11

这是一个更完整的解决方案,实际上处理了 bash 完成垃圾邮件垃圾。

需要明确的是:我在这里除了研究什么也没做。所有功劳都归功于Marius Gedminas

这对我来说非常适合 Gnome-Terminal/Terminator(把它放在你的 .bashrc 或正在获取的地方)

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

    # Show the currently running command in the terminal title:
    # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
    show_command_in_title_bar()
    {
        case "$BASH_COMMAND" in
            *\033]0*)
                # The command is trying to set the title bar as well;
                # this is most likely the execution of $PROMPT_COMMAND.
                # In any case nested escapes confuse the terminal, so don't
                # output them.
                ;;
            *)
                echo -ne "\033]0;${USER}@${HOSTNAME}: ${BASH_COMMAND}\007"
                ;;
        esac
    }
    trap show_command_in_title_bar DEBUG
    ;;
*)
    ;;
esac
Run Code Online (Sandbox Code Playgroud)

这也是一个交叉帖子,因为我刚刚发现它并想分享,我认为它在这里也很有用。


小智 10

这已经在这里得到了某种回答。

  • trap 'command' DEBUG使 bashcommand在每个命令之前运行。
  • echo -ne "\033]0;Title\007" 将标题更改为“标题”
  • $BASH_COMMAND 包含正在运行的命令。

结合这些我们得到

trap 'echo -ne "\033]0;$BASH_COMMAND\007" > /dev/stderr' DEBUG
Run Code Online (Sandbox Code Playgroud)

然后我们只需在完成命令后重置标题即可。我通过设置$PS1将标题更改为当前路径来做到这一点。

tl;dr:将这两行(按这个顺序,否则我得到一个乱码提示)添加到底部 ~/.bashrc

PS1="\033]0;\w\007${PS1}"
trap 'echo -ne "\033]0;$BASH_COMMAND\007" > /dev/stderr' DEBUG
Run Code Online (Sandbox Code Playgroud)

编辑:您$PS1可能已经更改了标题,在这种情况下,只需要最后一行。