1

I have a habit of hitting Ctrl+Alt+T to show my terminal when using Ubuntu (GNOME). After a while I have a plethora of terminal windows open, when all I really need is one.

How can I make Ctrl+Alt+T not necessarily open a new terminal window, but show the one I have already opened?

The logic should be like this: If a terminal is already opened, show it. If no terminal is open, open a new terminal (equal to default behaviour).

birgersp
  • 441
  • 1
  • 7
  • 19

5 Answers5

4

Save this as a script, but change all occurrences of xfce4-terminal with your regular terminal name:

#!/bin/bash

if [[ $(pgrep -x xfce4-terminal) ]]; then xdotool windowactivate xdotool search --pid $(pgrep -x xfce4-terminal) | tail -1; else xfce4-terminal; fi

And add a shortcut that executes that script. This might differ depending on your OS.

On Xubuntu you can enter the path to the script into Keyboard/Application Shortcuts.

You might need to install xdotool if you haven't already:

sudo apt install xdotool

In my tests this opened the terminal in which you executed the last command.

Natan
  • 817
4

Option 1: keybinding to activate pinned icon

One way that will work on any standard Ubuntu desktop, whether it runs on Xorg or Wayland, is to bind an extra key to the build in functionality to switch to a pinned icon on the dash. By default, Super+1 activates, or starts if it is not yet running, the application pinned first.

Suppose your terminal is pinned as the fourth icon on the dock. By default, you can switch to it hitting Super+4. To also switch to it using Control+Alt+T, first deactivate the default binding ("Settings", "Keyboard", "Keyboard shortcuts", "Launch terminal" under "Launchers"). Then either use dconf-editor to add a key, or use the terminal:

gsettings set org.gnome.shell.keybindings switch-to-application-4 "['<Super>4','<Control><Alt>t']"

Option 2: Using the Gnome Shell extension Run or raise

Run or raise can be configured with much more control, and independent on the order of the icons in the dash, using the very well maintained Gnome Shell extension Run or raise by e2rd.

For users of default Gnome Shell: it allows also to bring a single window to the front, whereas the previous option will behave as if you clicked the icon on the dash, i.e., bring all windows of the application in front.

Option 3 (Xorg only)

For systems running on Xorg, the classical Xorg tools continue to do their excellent job. Delete the default keybinding to launch the terminal (see above) and create your custom version, assigning the command:

sh -c "wmctrl -x -a Gnome-terminal || gnome-terminal"

First part checks if a window of class "Gnome-terminal" exists and activates it. If that fails (||) the second command is attempted, i.e, launching the terminal.

For the same but on steroids, you can use the excellent bash script jumpapp instead. It uses wmctrl under the hood, but allows to cycle between different open windows by repeatedly hitting the shortcut key, workaround situations where the window is closed, but a background process remains running for some time, etc. (I am looking at you, nautilus...)

As elaborated in Natan's answer, also xdotool can be used. Also that option is only working on Xorg.

vanadium
  • 88,010
1

I currently do this in Manjaro Gnome 42 Wayland.

No reason it shouldn't work in Ubuntu based derivatives.

This is to have a shortcut that will focus a current gnome-terminal otherwise it will spawn a new instance.

  1. Load this Extension https://extensions.gnome.org/extension/5185/toggle-window/

  2. Create a Keyboard shortcut with the following.Yes that is all one line.

bash -c "pgrep -x gnome-terminal- >/dev/null && gdbus call --session --dest org.gnome.Shell --object-path /cn/extensions/toggleWindow --method cn.extensions.toggleWindow.ToggleWindowByWMClassName 'gnome-terminal' || gnome-terminal"

I haven't found much else that work reliably for Wayland and Gnome 41+ so thought I'd share it somewhere. The other methods using wmctrl etc. will not work on Wayland.

beanrod
  • 11
0

I want to add exact steps, for anyone finding this later.

  1. Add this script ~/.scripts/open-terminal:

    #!/bin/bash
    

    PID=$(pgrep -x gnome-terminal-) if [[ $PID -ne "" ]] then xdotool windowactivate xdotool search --pid $PID | tail -1 else gnome-terminal fi

  2. Make the script executable: chmod +x ~/.scripts/open-terminal

  3. Go to the keyboard shortcuts and add the script to a key of your choosing (mine being Ctrl+Alt+T)

  4. Your hotkey should be ready

    enter image description here

muru
  • 197,895
  • 55
  • 485
  • 740
birgersp
  • 441
  • 1
  • 7
  • 19
  • Adding on @birgersp You can use this, and this works for any terminal emulator, be it terminator or konsole. `#!/bin/bash

    PID=$(pgrep x-terminal-emul) if [[ $PID -ne "" ]]; then xdotool windowactivate "$(xdotool search --pid "$PID" | tail -1)" else x-terminal-emulator fi`

    – hiru007 Jul 12 '23 at 16:47
0

I tried to use xdotool like the top answer suggested, but it didn't work for gnome-terminal (I kept getting a variety of errors; it worked for some other windows btw). I didn't want to install any extensions, so I just installed tilix which looks pretty similar to gnome-terminal and has an option to focus on existing window in the preferences instead of opening a new one. I then assigned a shortcut to run tilix command in custom shortcut.

bkc4
  • 1