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

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

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

要明确:我自己在这里没有做任何事情,只是研究。 所有功劳归功于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 

这也是一个交叉post,因为我刚刚发现了它并希望分享,我认为它在这里也很有用。

这在这里有点回答。

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

结合这些,我们得到

 trap 'echo -ne "\033]0;$BASH_COMMAND\007" > /dev/stderr' DEBUG 

然后我们只需在完成命令后重置标题。 我这样做是通过设置$PS1将标题更改为当前路径。

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

 PS1="\033]0;\w\007${PS1}" trap 'echo -ne "\033]0;$BASH_COMMAND\007" > /dev/stderr' DEBUG 

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