4

Is there a way to get Ctrl+Alt+T to open a terminal tab, if you already have an open terminal, instead of another terminal window?

I have already changed my preferences to open new terminals in tab. (I still want the key binding to open a terminal if none are open.)

1 Answer 1

3

Here is what I've done and it did the job pretty good.

  1. change gnome-terminal preferences to default if you have changed it.
    so, set the Ctrl+Shift+t as shortcut for opening a new tab.

    enter image description here

  2. We need xdotool (it's around 30KB) install it via:

    sudo apt install xdotool
    
  3. Now create a file (e.g in your home named .custom-terminal-key.sh) and put these lines in it (Thanks to Jacob Vlijm):

    #!/bin/bash
    
    if [[ "$(xprop -id "$(xdotool getactivewindow)" WM_CLASS)" == *"gnome-terminal"* ]]; then
      sleep 0.1
      xdotool key ctrl+shift+t
    elif ! pgrep 'gnome-terminal'; then
      gnome-terminal
    fi
    

    Every time we run it, if any gnome-terminal was open, it will simulate a Ctrl+Shift+t key binding, otherwise it runs gnome-terminal.

  4. Finally we change the default behavior of Ctrl+Alt+t, instead of opening a terminal every time you press these, it will run our script.

    gsettings set org.gnome.desktop.default-applications.terminal exec '/home/USER/.custom-terminal-key.sh'
    

    Change USER with your real username.

  5. Don't forget to give our script the executable bit:

    chmod +x ~/.custom-terminal-key.sh
    

We are done.


Rollback

Whenever you changed your mind just run:

gsettings set org.gnome.desktop.default-applications.terminal exec 'gnome-terminal'

Remove our script rm ~/.custom-terminal-key.sh and xdotool: sudo apt remove xdotool.


Getting active window name

11
  • maybe add a condition to check if the gnome-terminal window is the active one, raise it if not. Else the command might end up on the wrong application. Apr 27, 2017 at 18:13
  • Still it works when the window is active, but not if it isn't. I don't think the answer you use in the link is the best on that question though :) Apr 27, 2017 at 19:58
  • Yeah, and I think it shouldn't; About finding the window name, I used it to get the process name (command) easier ;)
    – Ravexina
    Apr 27, 2017 at 20:03
  • Bash is not really my thing, but if that's your decision, this paste.ubuntu.com/24468812 might be shorter. You can simply chain xdotool window commands. Apr 27, 2017 at 20:25
  • Ah wait, you'd still have to launch the terminal if not running: paste.ubuntu.com/24468949. Furthermore, I don't have the key org.gnome.desktop.default-applications.terminal (unity/16.04) You might want to mention the manual way, using Settings > keyboard. Apr 27, 2017 at 20:58

You must log in to answer this question.

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