3

I know that gnome-terminal will open a new window, I wondered is there a way to open a new tab, not just a new window?

After this, is there a way to pass a command to the new window for example

gnome-terminal && cd /

Which will result in

  1. New Terminal Opens
  2. New Terminal then runs cd /

[Edit] Will these answers will say how to pass cmds to another terminal, it does not answer how to open a new TAB. Which is what I am after, I do not want more than one terminal open.

1

2 Answers 2

6

GNOME Terminal offers no generic command line solution to open a new tab in an already existing window.

(Why? Because it may have multiple windows open, and it wouldn't know in which one to open the new tab. You might say you only have one window, but if such a feature was implemented, the software would need to be consistent and need to do something reasonable when it had multiple windows, which it couldn't really do.)

Hacks, such as simulating the user pressing a key, exist as per an already filed answer, but work only under X.Org, not under Wayland.

Beginning with forthcoming version 3.28, the command gnome-terminal --tab will open a new tab in the GNOME Terminal window where this command is executed from, if this command is executed from within GNOME Terminal.

(How does it know in which window to open the new tab in this case? It knows by setting a certain environment variable upfront to a different value in each of the terminals it opens. That environment variable lets it locate the desired window.)

So for your particular case, you could do:

gnome-terminal --tab -- bash -c 'cd /; exec bash'

This way you get a running shell in the new tab once the cd / is completed. If you just did gnome-terminal --tab -- some command, then the tab will close immediately after some command exits. By using bash -c 'some command; exec bash' instead, the tab will have a running bash process once some command completes.

3

You must simulate pressing shift + ctrl + T in the terminal. Programs exist to doing that:

If you want to do it from the command line

For example you can use xdotool - a program that lets you simulate keyboard input.

sudo apt-get install xdotool

then type in the terminal:

xdotool key ctrl+shift+t

That will simulate pressing the key combination.

2
  • But then how do I pass a command to the newly opened window? Thanks btw Jan 26, 2018 at 14:37
  • If it's possible you can add the command to your ~/.bashrc file. Every time terminal opens this file is invoked.
    – Mahdi
    Jan 26, 2018 at 14:45

Not the answer you're looking for? Browse other questions tagged .