Ctrl + Alt + T
opens a new terminal window. How do I make a keyboard shortcut open an existing window for an application if one is already open (and open a new one if not)?
Ctrl + Alt + T
opens a new terminal window. How do I make a keyboard shortcut open an existing window for an application if one is already open (and open a new one if not)?
The basic way to run-or-raise an application, i.e., switch to it if it is running, else run it, is a one-line terminal command. It uses wmctrl
. Install it with sudo apt install wmctrl
. Then enter the following command for your shortcut key:
sh -c "wmctrl -x -a Gnome-terminal || gnome-terminal"
wmctrl
checks if a window with class Gnome-terminal
exists. If that fail (||
) the second command is executed, which is just launching a new instance of gnome-terminal. sh -c
is needed to wrap it all in a single command.
You can find the window class of an open window by running xprop | grep WM_CLASS
in a terminal, then clicking on the window.
You can delete the default Ctrl+Alt+t keybinding and replace it by your own custom version.
An option on steroids that I use is a script jumpapp. The GitHub page explains how to install it in Ubuntu as a .deb package, so it is registered in the package management system. The command for your shortcut key becomes as simple as:
jumpapp gnome-terminal
The added value over the basic way is that jumpapp
will allow you to cycle through multiple open windows when you repeatedly hit the shortcut key, and can accomodate some specific cases. Read all about it in the excellent man
page, man jumpapp
.