How to open a new tab in GNOME Terminal from command line?
Solution 1
#!/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 2
You can also have each tab run a set command.
gnome-terminal --tab -e "tail -f somefile" --tab -e "some_other_command"
Solution 3
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
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
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.)
Comments
-
Vikrant Chaudhary over 3 years
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
? -
Vikrant Chaudhary over 14 yearsThanks, 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
-
Jeffrey Jose about 14 yearsif this works that'll put an end to so many months of frustration. Thanks a lot.
-
Cerin over 13 yearsxdotool, great resource. I was looking for the equivalent of Window's AutoIt, but for Linux.
-
Calin about 13 yearsThanks 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?
-
umpirsky almost 13 yearsI have nautilus script which opens current location in remote terminal github.com/umpirsky/nautilus-scripts/blob/master/…. I would like to open it in existing terminal if one exists. Is there any way to combine it with this solution?
-
Hedgehog over 12 yearsI get 'There was an error creating the child process for this terminal' in response to
gnome-terminal --tab -e "cd /tmp"
-
glarrain about 12 years@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. -
Adrian Matteo almost 12 yearsI'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?
-
user13107 over 11 years@Calin use
sleep 1; xdotool type --delay 1 --clearmodifiers "your Command"; xdotool key Return;
to run a command. -
IDDQD about 11 years@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. -
Klaus almost 11 years@AdrianMatteo See stackoverflow.com/questions/17402152/…
-
Muthu Ganapathy Nathan over 10 yearsThey are asking command line ., to automate the things. These shortcuts seldom helps.
-
Chris Morgan about 10 yearsWhy the WID and windowfocus bit? Won’t the window already be focused?
-
Steffen Winkler over 9 yearsthat 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.
-
Muthu Ganapathy Nathan over 9 years@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 ;)
-
EoghanM about 8 yearsThis 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 -
Barry Staes over 7 yearsThese quotes
'command 1'
work better than double quotes which only work for me when i also specify--working-directory="/some/path/"
-
WillC about 7 yearsExample 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.
-
nhed about 7 yearsSounds like this is very OSX centric answer when the question is clearly about (gnome) terminal in linux.
osascript
is OSX(Apple) -
Michael Scheper about 6 years@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. -
Dawoodjee about 6 yearsWhere do I place this script?
-
user000001 almost 6 yearsThis 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, likegnome-terminal --tab -- guard; gnome-terminal --tab -- rails runner MonitorRedisJob.perform_now
-
ATorras almost 6 yearsIn Ubuntu 16,
xdotool key 'ctrl+shift+t'
will do the job (in the current terminal) -
saiyan boy sunny over 4 yearsbut xdotool requires to not touch computer till it finishes otherwise it keeps writing wherever focus is
-
Jankapunkt about 4 yearsWorks the same for
xfce4-terminal
btw. Which of these commands is actually responsible for the pointand having them stay open...
? Iam asking because this is not mentioned in the manpage at least forxfce4-terminal
-
Frank Nocke almost 4 years@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 typeexit
.)