Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Switch Focus to a Gnome Terminal Tab via Script or Shell Command

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?

like image 479
Todd Avatar asked Oct 29 '25 22:10

Todd


2 Answers

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
like image 183
WinEunuuchs2Unix Avatar answered Nov 01 '25 14:11

WinEunuuchs2Unix


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

like image 20
m4110c Avatar answered Nov 01 '25 14:11

m4110c