Focus Terminal

Guake is a nice terminal, especially on laptops, where screen space is scarce. My favorite feature of guake is how it can be toggled in and out of focus with a key press (I use F9) to be both instantly accessible, and yet never in the way. But on my desktop, I'd like a windowed terminal, so the question arises: how do to focus to a specific window with a hotkey? – Not just open a new terminal, but focus to an already open terminal.

First up: other options. I tried tilda and some other terminals. Aside from tilda and guake, there are a few terminals that provide hotkey show/hide functionality, but I didn't particularly like them. I'm basic – on my desktop I like ubuntu/gnome and gnome-terminal. So how do I make a hotkey access for gnome-terminal?

Well.... it got a bit complicated. I tried wmctrl and xdotool, but they're tricky and wayland seems to want to kill tools like this "for security." Why you gotta be this way, wayland? Here's an interesting thread: wayland is broken by design which basically says wayland's aggressive concern for security makes simple tasks such as screen shots impossible. Of course that's an exaggeration, but wayland has certainly been disruptive, to say the least. Linux has always had a good security focus, but this feels over the top. If we can't take screenshots because "security," we're doing it wrong.

In spite of these difficulties, I found a pretty clean solution. Or at least it functions pretty nice. The script to make it work is a bit of a mess, but hopefully I won't have to touch it much.

First, Go to settings -> keyboard -> shortcuts -> customize shortcuts and create a shortcut for a script that will activate (or open) the terminal window.

Then write the script:

#!/bin/sh
GOTT=$(gdbus call \
  --session \
  --dest org.gnome.Shell \
  --object-path /org/gnome/Shell \
  --method org.gnome.Shell.Eval "
var mw =
  global.get_window_actors()
    .map(w=>w.meta_window)
    .find(mw=>mw.get_wm_class().includes('gnome-terminal-server'));
mw.activate(0)")

echo $GOTT |grep true >/dev/null ||gnome-terminal
# Remember to make it executable: chmod +x ~/.term

Bingo-bango! Now hitting F9 will cycle through the open gnome-terminals, or open a new gnome-terminal. Magic!

This uses gnome's GJS. Another resource is writing shell extensions. Gnome also provides the looking glass debugger, which can be accessed by hitting Alt+F2 and entering lg.

Rumor is eval may break in gnome.41 for security reasons, but I'll cross that bridge later. It may demand writing an extension. Here's a small sample of something similar: move/resize.