4

I am currently writing a script to open several tabs in gnome-terminal, and set their titles. I can open multiple tabs, but I need to change focus to those tabs (programatically) in order to set their titles from my script.

I use zsh and bash interchangeably, so any bash commands should work fine. I am starting to get familiar with xdotool and wmctrl, but unsure of a combo of commands to switch focus to an open tab.

What commands can I use to "switch to next open tab" or "switch to tab N" from a gnome-terminal CLI?

Todd
  • 2,824
  • 2
  • 29
  • 39

3 Answers3

3

In order to send the signal from Bash Shell use xdotool:

sudo apt install xdotool

In your script issue this command:

xdotool key Control+Page_Up
WinEunuuchs2Unix
  • 1,801
  • 1
  • 17
  • 34
  • Thanks - this does not switch focus, however. Subsequent commands will all run in the original tab. My goal is to run commands in the new tabs that open. – Todd Sep 06 '19 at 22:56
1

You could just set the title of the tabs when opening them:

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...

I would have posted as a comment, but don't have enough reputation for that, yet

m4110c
  • 427
  • 4
  • 10
  • 1
    I think this is a perfectly viable alternative, and it solves my problem, even if it doesnt answer the question exactly. Id say it warrants its own answer and an upvote, thanks :) – Todd Aug 17 '19 at 17:49
  • 1
    Yep, I realized that, that's why I would have commented instead of answered...But now, thanks to your (and others) upvote I gained the right to do so. Yay! :-) – m4110c Aug 18 '19 at 00:19
0

I solved this with xdotool

On one line, first, open a new tab with the key command. The default behavior is to switch focus to this tab. Then, use the type command to run a function, script, or other program in the new tab. Last, use the key command to "press enter." Repeat for N-many tabs!

# 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
}
Todd
  • 2,824
  • 2
  • 29
  • 39