134

I'm using Ubuntu 9.04 x64 and when I write:

gnome-terminal --tab

At the terminal, I expect it to open a new tab in the same terminal window. But it opens a new window instead.

I found out that its intention is to open a new tab in a new window, i.e., if I write:

gnome-terminal --tab --tab

It will open a new window with two tabs.

So, the question is, how can I open a new tab in the current window using a command in gnome-terminal?

6
  • 4
    if you are writing the gnome-terminal anyway, why cant you just press ctrl+shift+t to open up a new tab ;)
    – rasjani
    Commented Jul 29, 2009 at 9:41
  • 16
    And how am I supposed to press Ctrl+Shift+T when the command is being run from a script file? (Heard something called D-Bus can do that though)! Commented Jul 29, 2009 at 17:51
  • 24
    Whenever I start my PC, I need to open a few tabs in my gnome-terminal. And automatifying that will make me feel myself a bit more geeky. (As they say) Laziness is a programmer's feature. Commented Jul 29, 2009 at 17:55
  • @VikrantChaudhary threevirtues.com :-)
    – jpaugh
    Commented Feb 22, 2018 at 20:27
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.
    – jww
    Commented Sep 26, 2018 at 0:11

10 Answers 10

77

You can also have each tab run a set command.

gnome-terminal --tab -e "tail -f somefile" --tab -e "some_other_command"
6
  • 11
    I get 'There was an error creating the child process for this terminal' in response to gnome-terminal --tab -e "cd /tmp"
    – Hedgehog
    Commented Nov 2, 2011 at 3:01
  • 4
    @Hedgehog, I've a way for that: gnome-terminal --tab --working-directory="/home/user/X/Y/". I do not why, but "~/X/Y/" did not work.
    – glarrain
    Commented May 24, 2012 at 16:10
  • 1
    I'm having trouble with my commands, if I only use --tab it works but if I use --tab -e "my_bash_shorcut" it does not work. Do you know why? Commented Sep 21, 2012 at 11:45
  • @AdrianMatteo A bit late, but I think I've figured it out: if you make two files with gibberish and then run this command gnome-terminal --tab -e "tail -f file_a" --tab -e "tail -f file_b", the gnome terminal will open with two tabs where each tab will have respective file contents, but will close the moment you send ^C. This show you why it doesn't work, but I don't know how to remedy this.
    – IDDQD
    Commented Apr 2, 2013 at 16:05
  • 1
    This is good, unfortunately -e seems to be deprecated now, I get a warnging "Option “-e” is deprecated and might be removed in a later version of gnome-terminal.", but you can easily separate the commands, like gnome-terminal --tab -- guard; gnome-terminal --tab -- rails runner MonitorRedisJob.perform_now
    – user000001
    Commented Jul 4, 2018 at 9:28
72
#!/bin/sh

WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID

This will auto determine the corresponding terminal and opens the tab accordingly.

11
  • 4
    Thanks, works good. In proper form - WID= xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}'; xdotool windowfocus $WID; xdotool key ctrl+shift+t $WID Commented Feb 9, 2010 at 15:02
  • 6
    Thanks for the solution. Though, it's not clear to me how I can execute different commands in different tabs. No matter where I add the commands they all get executed in the first tab. Can you include a solution for this?
    – Calin
    Commented May 3, 2011 at 21:25
  • 11
    @Calin use sleep 1; xdotool type --delay 1 --clearmodifiers "your Command"; xdotool key Return; to run a command.
    – user13107
    Commented Oct 30, 2012 at 15:37
  • 3
    Why the WID and windowfocus bit? Won’t the window already be focused? Commented May 7, 2014 at 3:44
  • 1
    Where do I place this script?
    – Dawoodjee
    Commented May 6, 2018 at 14:45
8

I found the simplest way:

gnome-terminal --tab -e 'command 1' --tab -e 'command 2'

I use tmux instead of using terminal directly. So what I want is really a single and simple command/shell file to build the development env with several tmux windows. The shell code is as below:

#!/bin/bash
tabs="adb ana repo"
gen_params() {

    local params=""
    for tab in ${tabs}
    do  
        params="${params} --tab -e 'tmux -u attach-session -t ${tab}'"
    done
    echo "${params}"
}
cmd="gnome-terminal $(gen_params)"
eval $cmd
1
  • These quotes 'command 1' work better than double quotes which only work for me when i also specify --working-directory="/some/path/" Commented Oct 4, 2016 at 10:05
5

A bit more elaborate version (to use from another window):

#!/bin/bash

DELAY=3

TERM_PID=$(echo `ps -C gnome-terminal -o pid= | head -1`) # get first gnome-terminal's PID
WID=$(wmctrl -lp | awk -v pid=$TERM_PID '$3==pid{print $1;exit;}') # get window id

xdotool windowfocus $WID
xdotool key alt+t # my key map
xdotool sleep $DELAY # it may take a while to start new shell :(
xdotool type --delay 1 --clearmodifiers "$@"
xdotool key Return

wmctrl -i -a $WID # go to that window (WID is numeric)

# vim:ai
# EOF #
5

Just in case, you want to open

  • a new window
  • with two tabs
  • and executing command in there
  • and having them stay open...

here you go:

gnome-terminal --geometry=73x16+0+0 --window \
  --working-directory=/depot --title='A' --command="bash -c ls;bash" \
  --tab --working-directory=/depot/kn --title='B' --command="bash -c ls;bash"

(same for mate-terminal btw.)

2
  • Works the same for xfce4-terminal btw. Which of these commands is actually responsible for the point and having them stay open...? Iam asking because this is not mentioned in the manpage at least for xfce4-terminal
    – Jankapunkt
    Commented May 15, 2020 at 7:35
  • 1
    @Jankapunkt Hmm... Excellent question! What I found: a simple, parameterless „<guiXY>-terminal“ stays open, as soon as --command enters the game, it indeed closes after execution. This --command="bash -c ls;bash" thing, nested and with a trailing bash command seems to be the trick! (because that sub-bash stays open, thus the command never finishes. until you type exit.)
    – Frank N
    Commented Aug 20, 2020 at 14:37
2

To bring together a number of different points above, here's a script that will run any arguments passed to the script vim new_tab.sh:

#!/bin/bash
#
# Dependencies:
#   sudo apt install xdotool

WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID
sleep 1; xdotool type --delay 1 --clearmodifiers "$@"; xdotool key Return;

Next make it executable: chmod +x new_tab.sh

Now you can use it to run whatever you'd like in a new tab: ./new_tab.sh "watch ls -l"

0

I don't have gnome-terminal installed but you should be able to do this by using a DBUS call on the command-line using dbus-send.

0

Consider using Roxterm instead.

roxterm --tab

opens a tab in the current window.

0

For anyone seeking a solution that does not use the command line: ctrl+shift+t

4
  • 12
    They are asking command line ., to automate the things. These shortcuts seldom helps. Commented Nov 19, 2013 at 4:18
  • that was exactly what I was looking for. The question in the title is not limited to 'automated solutions' @EAGER_STUDENT and to be honest, I find it hard to come up with a use case where I need a GUI automated. That's like doing an open heart surgery through the spine. Commented Dec 13, 2014 at 20:49
  • 6
    @SteffenWinkler I'm glad that the solution helped you. But since the question said 'using command line' I assumed to use some automated commands, something like the high voted answers... Anyway the answer itself clarifies 'For anyone seeking a solution that does not use the command line'.... Kudos to the answer.... Moreover, 'I find it hard to come up with a use case where I need a GUI automated.'say always I need 5 tabs to be opened at startup. In that case, it would be useful. But in that case too we can use shortcut to automate anyway ;) Commented Dec 15, 2014 at 16:22
  • 2
    Example where I need automated tab-opening; I'm automating startup scripts on a robot, and I want to open tabs to run commands so that when the processes crash (as they all eventually do) I can ssh in from my laptop and have a look at the log output on the consoles without digging through the log file directories.
    – WillC
    Commented Apr 6, 2017 at 0:07
-2

For open multiple tabs in same terminal window you can go with following solution.

Example script:

pwd='/Users/pallavi/Documents/containers/platform241/etisalatwallet/api-server-tomcat-7/bin'
pwdlog='/Users/pallavi/Documents/containers/platform241/etisalatwallet/api-server-tomcat-7/logs'
pwd1='/Users/pallavi/Documents/containers/platform241/etisalatwallet/core-server-jboss-7.2/bin'
logpwd1='/Users/pallavi/Documents/containers/platform241/etisalatwallet/core-server-jboss-7.2/standalone/log'

osascript -e "tell application \"Terminal\"" \

-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $pwd\" in front window" \
-e "do script \"./startup.sh\" in front window" \
-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $pwdlog\" in front window" \
-e "do script \"tail -f catalina.out \" in front window" \
-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $pwd1\" in front window" \
-e "do script \"./standalone.sh\" in front window" \
-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $logpwd1\" in front window" \
-e "do script \"tail -f core.log \" in front window" \
-e "end tell"
> /dev/null 
2
  • 2
    Sounds like this is very OSX centric answer when the question is clearly about (gnome) terminal in linux. osascript is OSX(Apple)
    – nhed
    Commented Jun 9, 2017 at 20:22
  • 1
    @nhed: Thanks—that answers my what on earth is that?? response to this answer. Pallavi: Even if the question was about Mac, hardcoding your own home path doesn't make your answer very helpful. I suggest you use the $HOME environment variable, or the OSX equivalent if it's different. Commented Apr 18, 2018 at 22:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.