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

Jacob Vlijm
  • 82,471
  • 12
  • 195
  • 299
KT12
  • 225
  • 3
  • 10

1 Answers1

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

Jacob Vlijm
  • 82,471
  • 12
  • 195
  • 299
Ravexina
  • 54,268
  • 25
  • 157
  • 179
  • 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. – Jacob Vlijm Apr 27 '17 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 :) – Jacob Vlijm Apr 27 '17 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 '17 at 20:03
  • Bash is not really my thing, but if that's your decision, this http://paste.ubuntu.com/24468812/ might be shorter. You can simply chain xdotool window commands. – Jacob Vlijm Apr 27 '17 at 20:25
  • Ah wait, you'd still have to launch the terminal if not running: http://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. – Jacob Vlijm Apr 27 '17 at 20:58
  • @JacobVlijm thanks updated with a minor edit ;) Are you sure about the key? cause when I run : `gsettings set org.gnome.desktop.default-applications.terminal exec` it gives me `x-terminal-emulator` on 16.04 unity. – Ravexina Apr 28 '17 at 09:41
  • Huh? you are right! I must be blind or something, but I don't see it in dconf-editor. From cli, it is there. Anyway, nice concept :) Thanks for editing, +1 – Jacob Vlijm Apr 28 '17 at 09:48
  • Based on [this](https://askubuntu.com/a/465162/618307), I placed the .sh file in `usr/local/bin` and changed steps 4 and 5 accordingly. Unfortunately, couldn't get it to work. :/ – KT12 Apr 28 '17 at 16:11
  • @KT12 Do exactly as the instructions, use the full path. it'll works. – Ravexina Apr 28 '17 at 16:18
  • Unfortunately, couldn't get it to work, but since others did, will award the correct answer to yours. Thank you! – KT12 May 03 '17 at 01:05
  • @KT12 You should diagnose what is not working for you, e.g set another program (say `gedit`) instead of our script to see if our key binding are working ... first find which part does not work for you so we can work on it. – Ravexina May 03 '17 at 04:54