JREAM

Ubuntu Single Instance Hotkeys

Single Load Hotkeys in Ubuntu

Before we begin, make sure to remove any default shortcuts you may have. I’ll set my hotkey to CTRL + ALT + T to load the terminator emulator. We’ll use wmctrl (Window Manager Control) and a Bash script to execute this function. Finally, we’ll set the custom keybinding in the system settings.

Installing wmctrl

First, install wmctrl with the following command

sudo apt instlal wmctrl -y

If you have multiple terminals installed, this setup will open a new instance of your default terminal, and if there is an instance already open, it will bring that to focus rather than creating a new one.

If you want to change your default terminal emulator, use:

# If you want to change your default emulation
sudo update-alternatives --config x-terminal-emulator

Writing our Bash Script

Since you may be using different terminal emulators, this script will check for your system's default terminal. Below is an example that includes several terminal options.

Bash Script Example

You may need to modify two parts of the script indicated as [1] and [2] if you are adding another. The comments provide instructions on how to find a WM_CLASS.

#!/bin/bash

# Find the default terminal you have set so you don't have to rewrite this script
# The echo output of this function is stored in a DEFAULT_TERMINAL variable.
get_default_terminal() {
  # -------------------------------------
  # [1] Add any additional terminals here
  # -------------------------------------
  local terminals=("x-terminal-emulator" "gnome-terminal" "terminator", "warp", "wave")

  for terminal in "${terminals[@]}"; do
    if command -v "$terminal" > /dev/null; then
      echo "$terminal" # Places it in DEFAULT_TERMINAL below
      return  # Exit function after finding the terminal
    fi
  done

  echo "gnome-terminal"  # Fallback
}

DEFAULT_TERMINAL=$(get_default_terminal)
# echo "Using terminal: $DEFAULT_TERMINAL"

# --------------------------------------------
# [2] Add any WM_CLASS of your terminals here
# --------------------------------------------
# [help]  To find a Window Class name run this:
#         $ xprop WM_CLASS | awk -F'"' '{print $2"."$4}'
TERMINAL_CLASSES="gnome-terminal.Terminal|terminator.Terminator|x-terminal-emulator.X-terminal-emulator|dev.warp.Warp.dev.warp.Warp|wave.Wave"

# Check for an existing instance
WINDOW_ID=$(wmctrl -lx | grep -E "$TERMINAL_CLASSES" | awk '{print $1}')
# echo "Window ID found: $WINDOW_ID"

if [ -n "$WINDOW_ID" ]; then
  # echo "Focusing terminal window: $WINDOW_ID"
  wmctrl -ia "$WINDOW_ID"
else
  # echo "No terminal window found. Launching a new one."
  $DEFAULT_TERMINAL &
fi

Save Your File and Change Permissions

Save the script in your preferred location. For example, you can save it as ~/.local/bin/toggle-terminal.sh. Make sure to make it executable

chmod +x ~/.local/bin/toggle-terminal.sh

Set your Hotkey

Next, go to your Settings > Keyboard > View and Customize Shortcuts and Custom Shortcuts at the bottom. Add a new entry and point it to where you placed your file.

Sweet Icons
/home/jesse/.local/bin/toggle-terminal.sh

Try out your hotkey! If it doesn't trigger, I found that using ~/path/to/file.sh can be problematic; using the absolute path (e.g. /home/jesse/.local/bin/toggle-terminal.sh) usually works better.

If you need to debug any issues, uncomment the echo statements in the script and run it from a terminal that isn't your default terminal to see the output.

To log any output from your script when you trigger the hotkey, change your hotkey setting in the GUI Custom Shortcuts to:

/home/user/file.sh > ~/debug-toggle.log &2>1

After triggering it, you can check the log with:

cat ~/debug-toggle.log

Hopefully this is useful!