How to open a new tab in GNOME Terminal from command line?

LinuxUbuntuConsoleTerminal

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

You can also have each tab run a set command.

gnome-terminal --tab -e "tail -f somefile" --tab -e "some_other_command"

Solution 2 - Linux

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

Solution 3 - Linux

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

Solution 4 - Linux

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 #

Solution 5 - Linux

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

Solution 6 - Linux

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

Solution 7 - Linux

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"

Solution 8 - Linux

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.

Solution 9 - Linux

Consider using Roxterm instead.

roxterm --tab

opens a tab in the current window.

Solution 10 - Linux

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 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVikrant ChaudharyView Question on Stackoverflow
Solution 1 - LinuxChase SeibertView Answer on Stackoverflow
Solution 2 - Linuxutkarsh dubeyView Answer on Stackoverflow
Solution 3 - LinuxjaogoyView Answer on Stackoverflow
Solution 4 - LinuxjnoView Answer on Stackoverflow
Solution 5 - LinuxFrank NockeView Answer on Stackoverflow
Solution 6 - LinuxJeminiView Answer on Stackoverflow
Solution 7 - LinuxNeoGeekView Answer on Stackoverflow
Solution 8 - LinuxMike McQuaidView Answer on Stackoverflow
Solution 9 - Linuxuser25643View Answer on Stackoverflow
Solution 10 - LinuxPallavi VarneView Answer on Stackoverflow