Use RoxTerm instead – 'roxterm --tab'
opens a new tab in an existing terminal window, with no hack described here.
GNOME terminal has great support for tabbed terminal sessions. You can simply open a new tab by pressing CTRL+SHIFT+T and it’s very convenient.
However, it seems like there’s no straightforward way to ask gnome-terminal
command to reuse an existing window and add a new tab there. I tried various options like --tab
, but they didn’t work as I expected. I just want to keep only one terminal window in my desktop, but it looks like there’s no command line option that does the job.
So, I wrote a shell script that adds a new tab to an existing GNOME terminal window when there is already a running instance. It also launches a new terminal window if necessary:
#!/bin/bash
# Path: /usr/local/bin/gnome-terminal
if [ "x$*" != "x" ]; then
/usr/bin/gnome-terminal "$@"
else
pgrep -u "$USER" gnome-terminal | grep -qv "$"
if [ "$?" == "0" ]; then
WID=`xdotool search --class "gnome-terminal" | head -1`
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID
else
/usr/bin/gnome-terminal
fi
fi
This script looks for an existing gnome-terminal window, sends CTRL+SHIFT+T key event there, and raises the terminal window. Please note that xdotool and wmctrl are required to run the script. They should be available in most Linux distributions.
It’s just a band-aid solution – I hope I can get rid of this script from my system when the next release of GNOME terminal is out.
For reference — here’s one I wrote for KDE:
http://taint.org/2007/01/05/170825a.html
note that it also allows you to specify what command to run! very handy, e.g. “knewtab radish ssh radish”.
@Justin Mason: knewtab looks great! I just wonder why GNOME terminal doesn’t have a DBus interface..
You don’t need wmctrl for this particular task, since recent versions of xdotool support EWMH. You can use ‘xdotool windowactivate {windowid}’ (which I think should do the same thing as wmctrl -a)
More things need good dbus interfaces 😉
I’ve updated your version to take an optional command.
Doing something like:
./gterm.sh “less /etc/motd”
now works, if you need it to. The implementation is kinda hacky, though, but it works.
http://www.semicomplete.com/scripts/gterm.sh
@Jordan Sissel: I had to use wmctrl because Fedora 10 doesn’t ship the latest xdotool unfortunately. It’s xdotool-20071230-2.fc10.x86_64 now. Would be great if the latest version is shipped!
Wow, it’s hacky indeed. Cool idea though. 😀
hi.tried your script seems to work, but I get this error, nevertheless:
/opt/scripts/gterm.sh
usage: windowfocus wid
wmctrl: invalid option — –
WID=‘xdotool search — class “gnome-terminal” | head -1’
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID
whats wrong here?
also, your script does not work with different workspaces.
if I try to open a terminal there, nothing happens
if I open the script with xterm, I get
/opt/scripts/gterm.sh
usage: windowfocus wid
^Twmctrl: invalid option — –
any help would be appreciated.
@cellstorm: For some reason, it seem like some characters have been stripped out and translated to wrong characters when you copy and paste. Please make sure that double quotes, quotes, and hypens are correctly copied and pasted.
i have stopped using gnome terminal since i have discovered Terminator. tabs are ok, but i find switching between them inconvenient. With Terminator, i can split the current window and then i can see both sessions. Terminator can be found at: http://www.tenshu.net/terminator/
Useful trick
Life is easier this way. 🙂
I used a script doing something similar but no so powerful like yours combined with Sissel.
Thanks both
Thank you for articles
Hi,
Iff I want do it in gnome session how do I go about it? Can you please paas me the code to open a gnome-teminal and within that I need to open a 1 screen seseeion with 5 screen window and in those screen session all have to be chrooted to different slice/parttion OS along with gnome-teminal title of that particular OS.
Am I clear?
A bit explation:
I have 5 or more slices…and GNU/Linux OS in every slices. Now I may boot up one OS at a time and once I get into a desktop of gnome and fire gnome-terminal it will open an teminal(fire “screen”) and create a screen within that with 5 ot more window of screen and Chrooted to five different OS on other slices(i.e chroot /Arch /bin/bash). So the screen title would change to that perticular OS,instead of doing it manually like CTRL-A a…
PS: I have script to mount all the other slices of partition after boot.So chroot to those OS will be easy.But I want to automate that once I clicked into gnome-terminal icon and its open up.
I think almost an idea you posted here with few tweak.But need to get the exact code to work.
/dev/sda1——-> Gentoo
/dev/sda2——->openSUSE
/dev/sda3——>Arch
/dev/sda4——>Slackware
/dev/sda5——>CentOS
/dev/sda6——>Ferdora
/dev/sda7——>Ubuntu
Thanks in advance Mitch.
Thanks
Bhaskar
This one liner saved me. Its for Konsole (kde term)
for i in `awk -F “ “ ‘{print $1}’ list-of machines`;do konsole —new-tab -e ssh $i;done;
pretty fast.This would work if you install Konsole on gnome as well.
=Deep
sorry forget to mention, the above script with open new tabs with ssh session to all the servers in the file list.
Man, great solution!
My favorite terminal in tabs!
Just small note, replace:
#!/bin/sh
with
#!/bin/bash
because on debian there are some shell incompatibility issues.
Fixed. Thanks!
Any idea how to open the new tab with a specific profile and command (in this case, a ssh session)?
Hi,
I am trying to apply this to Xfce terminal on Fedora 18,
and when I tryed to run the command by hand, I found the following problem:
$ xdotool search —class “Terminal”
60817409
60831550
60817412
$ xdotool windowfocus 60817409
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 42 (X_SetInputFocus)
Serial number of failed request: 17
Current serial number in output stream: 19
Any idea where is the problem?
I guess the problem is that only one window ID out of the three in the search result works. Specifying 60817412 or 60831550 will work in this case. I couldn’t find a way to narrow down the search result yet though.
I can’t get anything to work! I tried this script, and with a terminal window open, it creates a blank terminal that won’t close. Without a terminal window open, it creates a terminal window, but byobu doesn’t do its thing. All I want to do is create a freaking script to open a command in a new tab. I don’t want a script to create a new blank tab. I want to be able to pass an argument to a script and have it open a tab with that argument being the command to execute.
Hi Tim,
I don’t use this script anymore. I just use RoxTerm with the ‘–tab’ option, and it just works. 🙂