Opening a new tab in an existing GNOME terminal window

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:

# Path: /usr/local/bin/gnome-terminal
if [ "x$*" != "x" ]; then
  /usr/bin/gnome-terminal "$@"
  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

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.

22 Comments Opening a new tab in an existing GNOME terminal window

  1. Jordan Sissel

    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 😉

  2. Trustin Lee

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

  3. cellstorm

    hi.tried your script seems to work, but I get this error, nevertheless:

    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

    usage: windowfocus wid
    ^Twmctrl: invalid option — –

    any help would be appreciated.

  4. Trustin Lee

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

  5. Bhaskar Chowdhury


    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







    Thanks in advance Mitch.


  6. Deep

    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.

  7. Deep

    sorry forget to mention, the above script with open new tabs with ssh session to all the servers in the file list.

  8. szel

    Man, great solution!
    My favorite terminal in tabs!

    Just small note, replace:

    because on debian there are some shell incompatibility issues.

  9. Jose Sa

    Any idea how to open the new tab with a specific profile and command (in this case, a ssh session)?

  10. Miro Hrončok

    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”
    $ 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?

  11. Trustin Lee

    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.

  12. Tim Ramich

    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.

    1. trustin

      Hi Tim,
      I don’t use this script anymore. I just use RoxTerm with the ‘–tab’ option, and it just works. 🙂

Comments are closed.