如何使我的终端的顶部显示正在运行的命令?


13

我经常并行运行需要很长时间才能完成的命令,有时我无法跟踪正在运行的命令,因为它们在屏幕上输出的信息基本相同。

您是否知道找出什么命令在哪个终端中运行的任何方式?


1
不知道这是否仍然有效,但是除了@dessert的出色答案之外,这也可能有所帮助。→ askubuntu.com
questions/

Answers:


12

取自Bash-通过运行第二个命令·U&L更新终端标题,并稍作更改:

trap 'echo -ne "\033]2;$(history 1 | sed "s/^[0-9 ]* \+//")\007"' DEBUG

这(ab)使用DEBUG信号作为触发器,通过XTerm Control Sequence用历史中的最后一个条目(即您执行的最后一个命令)更新标题。将行添加到您的行中,~/.bashrc以在每个新的终端窗口中启用该功能。

要在标题旁边打印其他命令输出,请说出当前目录,pwd后跟“:”和当前正在运行的命令,建议使用printf以下命令:

trap 'echo -ne "\033]2;$(printf "%s: %s" "$(pwd)" "$(history 1 | sed "s/^[0-9 ]* \+//")")\007"' DEBUG

一些终端仿真器允许您指定动态标题,甚至可以给您提供命令名称作为选项,这样您甚至无需摆弄-我在yakuake的个人资料设置中搜索并找到了它。


2

终端窗口标题可以通过更改变量的值$PS1-主提示字符串来更改。[1] [2]。我们可以将此解决方案与使用Dessert的答案中的命令的想法结合起来。 history


方法1:$PS1自动更新值。(更新)

将以下行添加到文件的底部~/.bashrc

# Change the terminal window title, based on the last executed command
rtitle() {
        # If the variable $PS1_bak is unset,
        # then store the original value of $PS1 in $PS1_bak and chang $PS1
        # else restore the value of $PS1 and unset @PS1_bak
        if [ -z "${PS1_bak}" ]; then
                PS1_bak=$PS1
                PS1+='\e]2;$(history 1 | sed "s/^[0-9 ]* \+//")\a'
        else
                PS1=$PS1_bak
                unset PS1_bak
        fi
};
export -f rtitle        # Export the function to be accessible in sub shells
#rtitle                 # Uncomment this line to change the default behaviour

然后source ~/.bashrc或者只是打开一个新终端并以这种方式使用该功能:

  • 执行rtitle以根据最后执行的命令自动开始更改终端窗口标题。
  • rtitle再次执行以返回默认行为。

方法2:$PS1手动更新值。(初始答案)

将以下行添加到文件的底部~/.bashrc

set-title() {                                                                                 # Set a title of the current terminal window
        [[ -z ${@} ]] && TITLE="$(history 2 | head -1 | sed "s/^[0-9 ]* \+//")" || TITLE="$@" # If the title is not provided use the previous command
        [[ -z ${PS_ORIGINAL} ]] && PS_ORIGINAL="${PS1}" || PS_ORIGINAL="${PS_ORIGINAL}"       # Use the original value of PS1 for each future change
        PS1="${PS_ORIGINAL}"'\e]2;'"$TITLE"'\a'                                               # Change the prompt (the value of PS1)
}; export -f set-title

然后source ~/.bashrc或者只是打开一个新终端并以这种方式使用该功能:

  • set-title <something>将终端窗口标题更改为<something>
  • set-title 不带参数的终端窗口标题将更改为上一个命令。

参考和示例:

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.