71

I installed xdotool by running sudo apt-get install xdotool and throw xdotool key ctrl+alt+t command to open a new terminal window from the current one.But it was not working.

What was the command to open a new terminal window from the current gnome-terminal?

Braiam
  • 67,791
  • 32
  • 179
  • 269
Avinash Raj
  • 78,556

3 Answers3

103

Just this command will do:

gnome-terminal

Normally if you want a command to open from the terminal and separate (so it returns to the prompt without having to close the opened program), you have to use something like this:

gnome-terminal & disown

However the parent terminal seems to detect that the same command is being used so you don't need to do that and gnome-terminal will suffice. This also seems to happen when running xfce4-terminal from Xfce's terminal, konsole from KDE's as well (doesn't seem to work when running xterm from xterm (see also xterm xterm) - Running konsole from Gnome/Unity & Xfce's terminal works as well, but for Xfce's terminal in gnome terminal you need xfce4-terminal & disown).

For more visit gnome-terminal's manual page:

 gnome-terminal  [-e,  --command=STRING]   [-x, --execute ]  [--window-with-profile=PROFILENAME]  [--tab-with-profile=PRO‐
       FILENAME]    [--window-with-profile-internal-id=PROFILEID]    [--tab-with-profile-internal-id=PROFILEID]    [--role=ROLE]
       [--show-menubar]   [--hide-menubar]   [--geometry=GEOMETRY]   [--disable-factory]  [-t, --title=TITLE]  [--working-direc‐
       tory=DIRNAME]  [--usage]  [-?, --help]
Wilf
  • 30,194
  • 17
  • 108
  • 164
  • 2
    You probably want to run it in the background, like that: gnome-terminal &. Otherwise the current terminal will be unusable, as it will be busy running that other one - so you end up with just one usable terminal, which may be missing the point. – Rafał Cieślak Jan 06 '14 at 19:59
  • On my Ubuntu 13.10 install, that did not appear to be a problem @RafałCieślak - I did think about that :-) – Wilf Jan 06 '14 at 20:02
  • 1
    Interesting. You are apparently right, however, I am not wrong neither :) I've just checked that in details. If I run gnome-terminal while another instance of it is already running (it may be the one I'm using to launch this command) - it indeed finishes immediately, because instead of running a new instance gnome-terminal, it tells that currently running one to open a new window. Tricky. But if I run gnome-terminal from anything else, and there are no other instances of gnome-terminal running, it does as I explained in the previous comment, blocking the terminal used to launch it. – Rafał Cieślak Jan 06 '14 at 20:25
  • 1
    @RafałCieślak - anyway, konsole doesn't seem to need at all... weird. I have no idea why this question/answer is so popular :) – Wilf Apr 22 '15 at 14:34
  • 2
    Thanks very much, if you want to open a terminal with the same directory you could do this, gnome-terminal . – Nasik Shafeek Jul 07 '15 at 04:58
  • 1
    If you are on Ubuntu MATE (e.g. 16.x) it's mate-terminal – Frank N Oct 27 '16 at 01:54
  • And for Unity (since ubuntu 17.10)? – JHBonarius Jan 19 '18 at 20:38
  • 1
    @JHBonarius (having not used a desktop version) doesn't it now come with Gnome by default, so it should be the same (or is there something else? Unity used to use gnome-terminal (and other Gnome apps like Nautilus), though they used to be modified in more recent versions. – Wilf Jan 19 '18 at 22:31
  • lubuntu 18.04. gnome-terminal and xfce-terminal seem to work except the colors are a bit off. – john-jones Mar 14 '21 at 21:35
11

Command to open new terminal window from the current terminal,

xdotool key ctrl+shift+n

To install xdotool,

sudo apt-get install xdotool
Avinash Raj
  • 78,556
1

The following script will open a new tab in the current gnome-terminal window and optionally give that tab a title. This works from any window, you don't have to be in a gnome-terminal window to run it. And, if there is no gnome-terminal running, it will start one. The only caveat is that if you changed the hotkey for opening a new tab you might have to change the line xdotool key ctrl+T to use your hotkey instead.

#!/bin/bash

DELAY=1
# get title we are going to set tab too, default to Terminal
title="Terminal"
if [ $# -eq 1 ]; then
    title="$1"
fi    

# get pid of running terminal server
TPID=$(ps -C gnome-terminal-server -o pid | tail -1 | sed -e's/\s//g')
if [ ${TPID} == "PID" ]; then
    # no terminal process running yet, so just start one
    gnome-terminal -t "$title" --tab
    exit 0
fi

# there is a terminal, get window id of the running terminal server
WID=$(wmctrl -lp | awk -v pid=$TPID '$3==pid{print $1;exit;}')
# get title of currently active tab
TTITLE=`xwininfo -id 0x5000006 | grep xwininfo | awk '{print $5;exit}'`
if [ "$TTITLE" == "\"Terminal\"" ]; then
    # so we don't go into an infinite loop later
    TTITLE="we had a terminal named terminal $$"
fi
# get focus on active terminal tab
xdotool windowfocus $WID
# use keyboard shortcut to open new tab
xdotool key ctrl+T

# see if we have created tab and are in terminal
NTITLE=`xwininfo -id 0x5000006 | grep xwininfo | awk '{print $5;exit}'`
waited=0
while [ "$TTITLE" == "$NTITLE" ]; do
    # sleep for 1 second before we try again
    xdotool sleep 1
    NTITLE=`xwininfo -id 0x5000006 | grep xwininfo | awk '{print $5;exit}'`
    if [ $waited == 0 ]; then
    echo "Waiting "
    waited=1
    fi
    echo -n "."
done    
if [ $waited == 1 ]; then
    echo ""
fi    

# active tab is the new one we created, wait DELAY seconds just to be sure we can type into it to set tab name
xdotool sleep $DELAY
xdotool type --clearmodifiers "termtitle $title"
xdotool key Return
# make tab the active window and raise it to top
wmctrl -i -a $WID
exit 0
seth
  • 171
  • 1
  • 7