My favorite shell scripts (part 1)

This is yet another “Linux Desktop is fun” post. Without further ado, let’s cut to the chase.

This will hopefully turn into a series of short shell scripts I find somewhat amusing in my everyday computer life.

You can look them up here.

Part 1: Activate single terminal instance, with single tmux session

This is the behaviour I wanted to emulate in KDE Plasma after leaving the iterm2 world. My muscle memory likes Meta + Esc to bring up the Terminal. It’s just very effortless having your left thumb on the CMD (also known as Meta) with left pinky reaching for ESC.

The combination should launch either a new instance or the already active one.

Perhaps some Linux terminals/desktop environments already implement that feature. I use alacritty on Plasma, because it feels like the most responsive one (without scientific benchmarks, I admit).

The default key shortcuts set up allowed me to spawn a new program instance every time the keystroke was made.

Anyway, here we go:

#!/usr/bin/env bash
WID=$(xdotool search $TERMINAL 2>/dev/null | sort | head -1)
if [ -z "$WID" ]
    $TERMINAL -e tmux -u
    xdotool windowactivate --sync $WID;



TERMINAL is defined upfront. As far as the Desktop Environment is concerned, it doesn’t matter. We obtain X11 Window ID by using xdotool searching for the particular $TERMINAL instance, sorting the results, so we can get somewhat predictable ordering (a weak assumption that we’ll always hit the oldest instance). head -n1 just takes the first one.

Trivia! xdotool is capable of much more, make sure to check man xdotool too!.

Having the non-existing Window ID, we just launch $TERMINAL making it execute tmux -u on startup. The -u flag is helpful for enforcing unicode support within the tmux session:

 -u   When starting, tmux looks for the LC_ALL, LC_CTYPE and LANG environment variables:
      if the first found contains ‘UTF-8’, then the terminal is assumed to support UTF-8.
      This is not always correct: the -u flag explicitly informs tmux that UTF-8 is sup‐

Otherwise, we have an existing Window ID. We use that to activate an existing X11 window, bringing up an already opened Terminal, hoping for the tmux session to be there. Worst case, we just launch tmux manually and pick a session we wanted to get back to.

DE Integration

That should be straight-forward to integrate with Plasma Settings at this point, just don’t forget to make the actterm script executable first.