【问题标题】:How to Switch Focus to a Gnome Terminal Tab via Script or Shell Command如何通过脚本或 Shell 命令将焦点切换到 Gnome 终端选项卡
【发布时间】:2018-08-31 20:34:07
【问题描述】:

我目前正在编写一个脚本来在 gnome-terminal 中打开几个选项卡,并设置它们的标题。我可以打开多个选项卡,但我需要将焦点更改为这些选项卡(以编程方式),以便从我的脚本中设置它们的标题。

我交替使用 zsh 和 bash,所以任何 bash 命令都应该可以正常工作。我开始熟悉xdotoolwmctrl,但不确定将焦点切换到打开选项卡的命令组合。

我可以使用哪些命令从 gnome-terminal CLI 中“切换到下一个打开的选项卡”或“切换到选项卡 N”?

【问题讨论】:

    标签: bash ubuntu gnome-terminal xdotool wmctrl


    【解决方案1】:

    为了从 Bash Shell 发送信号,请使用 xdotool:

    sudo apt install xdotool
    

    在您的脚本中发出以下命令:

    xdotool key Control+Page_Up
    

    【讨论】:

    • 谢谢 - 但是,这不会切换焦点。后续命令都将在原始选项卡中运行。我的目标是在打开的新标签页中运行命令。
    【解决方案2】:

    您可以在打开选项卡时设置它们的标题:

    gnome-terminal --geometry=80x25+0+0 --window --working-directory=<Firtst Tab Dir> \
                   --title='<First Tab Title>' --command="bash" \
                   --tab --working-directory=<Second Tab Dir> --title='<Second Tab Title>' \
                   --command="bash" and so on...
    

    我会作为评论发布,但还没有足够的声誉

    【讨论】:

    • 我认为这是一个完全可行的替代方案,它解决了我的问题,即使它没有准确回答问题。我说它需要自己的答案和赞成票,谢谢:)
    • 是的,我意识到,这就是为什么我会发表评论而不是回答...但是现在,感谢您(和其他人)的支持,我获得了这样做的权利。耶! :-)
    【解决方案3】:

    我用xdotool解决了这个问题

    在一行中,首先,使用key 命令打开一个新选项卡。默认行为是将焦点切换到此选项卡。然后,使用type 命令在新选项卡中运行函数、脚本或其他程序。最后,使用key 命令“按回车键”。重复 N 个标签!

    # inside a file loaded by .bashrc which contains all my functions:
    function setupterm() {
      # run a command, like set a title, in the current window/tab
      customCommandOne
      # do the needful as described above
      xdotool key Control+Shift+t && xdotool type customCommandTwo && xdotool key Return
      # repeat for n-many tabs
      xdotool key Control+Shift+t && xdotool type customCommandThree && xdotool key Return
      # return focus to first tab
      xdotool key Control+Page_Down
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多