Opening a new tab in an existing GNOME terminal window

2008-11-05 15:00

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/sh
# 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.

---

Comment

14 Comments

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 · 2008-11-06 23:49 · # · Reply

@Justin Mason: knewtab looks great! I just wonder why GNOME terminal doesn’t have a DBus interface..

Trustin Lee · 2008-11-07 03:05 · # · Reply

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 ;)

Jordan Sissel · 2008-11-09 15:27 · # · Reply

@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!

Trustin Lee · 2008-11-09 17:59 · # · Reply

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 · 2008-11-09 15:55 · # · Reply

Wow, it’s hacky indeed. Cool idea though. :D

Trustin Lee · 2008-11-09 18:01 · # · Reply

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 · 2009-02-01 23:43 · # · Reply

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

Trustin Lee · 2009-03-11 05:03 · # · Reply

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/

Saqib · 2009-07-29 01:23 · # · Reply

Useful trick

lucapette · 2009-10-26 18:56 · # · Reply

Life is easier this way. :)

I used a script doing something similar but no so powerful like yours combined with Sissel.

Thanks both

iñigo medina · 2009-12-11 16:00 · # · Reply

Thank you for articles

firma ekle · 2010-01-02 02:11 · # · Reply

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

Bhaskar Chowdhury · 2010-02-08 14:54 · # · Reply

 
  • Preview 버튼 누르고 reCAPTCHA 입력 후 Submit 버튼까지 눌러야 실제로 게시됩니다.
  • Make sure to answer the reCAPTCHA and click the Submit button to get your comment posted. It's not enough to click the Preview button only! -- See why.
---