133

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?

ib.
  • 27,830
  • 11
  • 80
  • 100
Vikrant Chaudhary
  • 11,089
  • 10
  • 53
  • 68
  • 4
    if you are writing the gnome-terminal anyway, why cant you just press ctrl+shift+t to open up a new tab ;) – rasjani Jul 29 '09 at 09: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)! – Vikrant Chaudhary Jul 29 '09 at 17:51
  • 23
    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. – Vikrant Chaudhary Jul 29 '09 at 17:55
  • @VikrantChaudhary http://threevirtues.com/ :-) – jpaugh Feb 22 '18 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](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Sep 26 '18 at 00:11
  • Your question **IS** the answer. Maybe it didn't work as you expected in the past, but it **does** now – Madacol May 09 '20 at 17:35

10 Answers10

75

You can also have each tab run a set command.

gnome-terminal --tab -e "tail -f somefile" --tab -e "some_other_command"
Chase Seibert
  • 15,703
  • 8
  • 51
  • 58
  • 11
    I get 'There was an error creating the child process for this terminal' in response to `gnome-terminal --tab -e "cd /tmp"` – Hedgehog Nov 02 '11 at 03: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 May 24 '12 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? – Adrian Matteo Sep 21 '12 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 Apr 02 '13 at 16:05
  • @AdrianMatteo See http://stackoverflow.com/questions/17402152/gnome-terminal-new-tab-with-alias-as-command-to-execute/17982997#17982997 – Klaus Aug 01 '13 at 00:04
  • 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 Jul 04 '18 at 09: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.

richq
  • 55,548
  • 20
  • 150
  • 144
utkarsh dubey
  • 875
  • 7
  • 19
  • 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` – Vikrant Chaudhary Feb 09 '10 at 15:02
  • if this works that'll put an end to so many months of frustration. Thanks a lot. – Jeffrey Jose Apr 24 '10 at 11:14
  • xdotool, great resource. I was looking for the equivalent of Window's AutoIt, but for Linux. – Cerin Dec 01 '10 at 13:35
  • 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 May 03 '11 at 21:25
  • I have nautilus script which opens current location in remote terminal https://github.com/umpirsky/nautilus-scripts/blob/master/Open%20in%20Remote%20Terminal. I would like to open it in existing terminal if one exists. Is there any way to combine it with this solution? – umpirsky Sep 07 '11 at 11:29
  • 11
    @Calin use `sleep 1; xdotool type --delay 1 --clearmodifiers "your Command"; xdotool key Return;` to run a command. – user13107 Oct 30 '12 at 15:37
  • 3
    Why the WID and windowfocus bit? Won’t the window already be focused? – Chris Morgan May 07 '14 at 03:44
  • This only works when executed from within `gnome-terminal` right? Is there a way of getting it to switch to terminal if e.g. executed as a script from within nautilus – EoghanM Apr 01 '16 at 13:41
  • 1
    Where do I place this script? – Dawoodjee May 06 '18 at 14:45
  • 1
    In Ubuntu 16, `xdotool key 'ctrl+shift+t'` will do the job (in the current terminal) – ATorras Jul 19 '18 at 08:53
  • but xdotool requires to not touch computer till it finishes otherwise it keeps writing wherever focus is – dgfjxcv Feb 18 '20 at 02:09
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
Rushyo
  • 7,495
  • 4
  • 35
  • 42
jaogoy
  • 81
  • 1
  • 2
  • These quotes `'command 1'` work better than double quotes which only work for me when i also specify `--working-directory="/some/path/"` – Barry Staes Oct 04 '16 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 #
jno
  • 997
  • 1
  • 10
  • 18
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.)

Frank N
  • 9,625
  • 4
  • 80
  • 110
  • 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 May 15 '20 at 07:35
  • 1
    @Jankapunkt Hmm... Excellent question! What I found: a simple, parameterless „-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 Aug 20 '20 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"

NeoGeek
  • 21
  • 2
1

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

Jemini
  • 51
  • 1
  • 12
    They are asking command line ., to automate the things. These shortcuts seldom helps. – Muthu Ganapathy Nathan Nov 19 '13 at 04: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. – Steffen Winkler Dec 13 '14 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 ;) – Muthu Ganapathy Nathan Dec 15 '14 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 Apr 06 '17 at 00:07
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.

Mike McQuaid
  • 9,615
  • 6
  • 33
  • 38
0

Consider using Roxterm instead.

roxterm --tab

opens a tab in the current window.

user25643
  • 97
  • 4
-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 
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
  • 2
    Sounds like this is very OSX centric answer when the question is clearly about (gnome) terminal in linux. `osascript` is OSX(Apple) – nhed Jun 09 '17 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. – Michael Scheper Apr 18 '18 at 22:53