How to choose a default tab focus in gnome-terminal? - linux

I have a bash script which opens several tabs inside one window and I want to choose one tabe to be the default focus
for example the below script opens 3 tabs on one window:
gnome-terminal --tab --geometry="100x20" --title="TAB1" -- bash -ic "command1"
gnome-terminal --tab --geometry="100x20" --title="TAB2" -- bash -ic "command2"
gnome-terminal --tab --geometry="100x20" --title="TAB3" -- bash -ic "command3"
So, let's say I want tab2 to be the focus after running the script and openning the window. Is there a way to specify that from the script?

I highly recommend installing terminator, it's a program which offers flexible management of multiple running gnome terminals. It allows you to choose a tab as the default focus while still viewing any other amount of terminals on the same view. It can be installed via:
$ sudo apt install terminator

Related

Opening new gnome-terminal (v3.28+) with multiple tabs and different commands

It seems the behavior of gnome-terminal has changed between the version shipped with Ubuntu 14 (v3.6?) and Ubuntu 18 (v3.28).
I have a script that opens a new gnome-terminal with a bunch of tabs setup to different directories for my development, and currently the first tab runs a script. The command to open the gnome-terminal with tabs looks something like this:
gnome-terminal \
--tab --command="myscript.sh" \
--tab --working-directory="<some dir 1>" \
--tab --working-directory="<some dir 2>" \
...
This works perfectly as desired in the gnome-terminal version that shipped with Ubuntu 14 (v3.6?).
But in the gnome-terminal version that ships with Ubuntu 18 (v3.28) several things have changed:
Unless I add the --window option, the tabs open in the current gnome-terminal, not a new one. Unfortunately adding the --window option opens an initial blank tab. Is it possible to open a new window with only the tabs that I specify?
I now get the following notice (though it functions as before):
# Option “--command” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
Changing my script per this guidance changes the behavior such that the command is issued to all tabs, whereas before I could apply a unique command to each tab. Does this mean the ability to run a separate command per tab has been deprecated, or am I missing something?
I appreciate suggestions on how to change my script to support the old behavior in the newer gnome-terminal.
1) Use --window for your first tab
gnome-terminal \
--window -t 'Tab 1' \
--tab -t 'Tab2' --working-directory="<some dir 1>" \
--tab -t 'Tab3' --working-directory="<some dir 2>" \
...
Unfortunately this will only allow one command to be passed in using the new design, and the window/tabs close at completion (I'm not sure if that was the behavior before)
2) If you don't care about the tab closing when the command is complete, you could do this:
$ gnome-terminal --window -- ./mytabs.sh
mytabs.sh
#!/bin/bash
gnome-terminal --tab -t 'Tab 1' -- ./myscript.sh
gnome-terminal --tab -t 'Tab 2' --working-directory="<some dir 1>"
gnome-terminal --tab -t 'Tab 3' --working-directory="<some dir 2>"
This will open each tab from the script in the window that was created in the code above it. It's a pain in that you either have to type out the first command or create a second script.

Start detached screen with ssh , without killing after the commands execute [duplicate]

I'm lazy, and I prefer that computers do my work for me. I ssh into several machines on a daily basis, so I created a simple script that launches some xterm windows and places them in positions I want (as you can see, I'm using bash):
#!/bin/bash
xterm -geometry 80x27+1930+0 &
xterm -geometry 80x27+2753+0 &
xterm -geometry 80x27+1930+626 &
xterm -geometry 80x27+2753+626 &
However, the next thing I do is go to the first window and type in
ssh server_a
then in the second
ssh server_b
and so on. What I'd like to do is have my script do the ssh commands in each xterm window, and then leave the windows open for me to do my work. I've seen the -e option for xterm, but the window closes after I execute my command. Is there a way to do this?
I apologize if this is a duplicate question. I've searched around and haven't had any luck with this. Many thanks!
I'd love to see a more elegant answer, but what I came up with does work:
xterm -e bash -c 'echo foo; exec bash'
Replace echo foo with the command of your choice, and you're good to go.
This answer gives one of the best answers I've seen so far to do this. Use the bash --init-file flag either in the shebang or when executing the terminal:
#!/bin/bash --init-file
commands to run
... and execute it as:
xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang
My only real complaint with the exec option is if the command executed prior to exec bash is long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.
Another option is cmdtool from the OpenWin project:
/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash
... where cmdtool injects the commands passed with -I to the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.
Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.
Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window
See: https://superuser.com/a/610048
"ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection"
https://github.com/duncs/clusterssh/wiki
$ cssh server_a server_b
$ command

How can I launch multiple xterm windows and run a command on each, leaving each window open afterward?

I'm lazy, and I prefer that computers do my work for me. I ssh into several machines on a daily basis, so I created a simple script that launches some xterm windows and places them in positions I want (as you can see, I'm using bash):
#!/bin/bash
xterm -geometry 80x27+1930+0 &
xterm -geometry 80x27+2753+0 &
xterm -geometry 80x27+1930+626 &
xterm -geometry 80x27+2753+626 &
However, the next thing I do is go to the first window and type in
ssh server_a
then in the second
ssh server_b
and so on. What I'd like to do is have my script do the ssh commands in each xterm window, and then leave the windows open for me to do my work. I've seen the -e option for xterm, but the window closes after I execute my command. Is there a way to do this?
I apologize if this is a duplicate question. I've searched around and haven't had any luck with this. Many thanks!
I'd love to see a more elegant answer, but what I came up with does work:
xterm -e bash -c 'echo foo; exec bash'
Replace echo foo with the command of your choice, and you're good to go.
This answer gives one of the best answers I've seen so far to do this. Use the bash --init-file flag either in the shebang or when executing the terminal:
#!/bin/bash --init-file
commands to run
... and execute it as:
xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang
My only real complaint with the exec option is if the command executed prior to exec bash is long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.
Another option is cmdtool from the OpenWin project:
/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash
... where cmdtool injects the commands passed with -I to the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.
Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.
Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window
See: https://superuser.com/a/610048
"ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection"
https://github.com/duncs/clusterssh/wiki
$ cssh server_a server_b
$ command

Using of gnome terminal to get new tab

I'm new to scripting.
I’m using the below code in my project, but the terminal is getting closed instantly. I want the hold the terminal and keep running
#!/bin/bash/
gnome-terminal --tab --working-directory="/home/sandhya/OpenBTS/public/openbts/trunk/apps/" -e "file = grep OpenBTS /home/sandhya/OpenBTS/public/openbts/trunk/apps \
if [ -f $file ] \
then \
echo " file exits" \
sudo "/home/sandhya/OpenBTS/public/openbts/trunk/apps/OpenBTS" \
fi"
I want the terminal to be stay and keep running the application.
Please correct me in case the method or syntax I used is wrong.
In gnome-terminal,
--go to Edit -> Profiles.
--Double click on Default profile
--Click on Title And Command tab.
--Select Hold Terminal Opens from when command exits list which located at bottom of window.
--close windows
Now run your script it will work
Or you can also add your profile and run script as follows
Click the New tab. and give your profile name
Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with
gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command
To make the terminal stay when the command exits:
In konsole there is a --noclose flag.
In xterm, there is a -hold flag.

How to open new tabs in gnome-terminal from script

I know I can right-click > open tab to open a new tab in gnome-terminal, but how can I do the same from a script? If i use 'gnome-terminal --tab-with-profile=...' it opens a new window.
I need this to be able to open multiple ssh sessions, in tabs, to servers I manage. I don't see any option in ssh to open new sessions in tabs.
I have two different gnome-terminal profiles, a profile 'local' i used for local terminals and a profile 'server' I use for terminals connected to production servers, so it is easy to see which is which. I could open a gnome-terminal, then right-click, open a tab, right-click again and set it to a different profile, then run an ssh command (with switches for non-standard port, key file etc) - but I'd like to condense this into an alias or script for convenience. Hope this clarifies the situation.
try this
//it opens up a window with a tab
gnome-terminal --window --tab
In the version of gnome-terminal in Ubuntu 14 (v3.6?), the command gnome-terminal --tab opens a separate window.
I'm not sure when the behavior changed exactly, but at least as of gnome-terminal v3.28 (Ubuntu 18), gnome-terminal --tab will open a new tab in the current terminal (despite the documentation saying it will open it in the most recently opened window). In this version the additional option --window is needed to open a separate window (ie. gnome-terminal --window --tab).

Resources