Custom Keyboard Shortcuts and wmctrl on Linux

Whether it’s the universal Copy-Paste combos or the often-used yet seldom consciously appreciated Undo command, we all use keyboard shortcuts regularly. Anyone who has to spend most of their time on the desk with this oldest computing input device has found the benefit of using such shortcuts. This is sadly(?) yet another reason why I abandoned the Microsoft Windows way of computing.

Increasingly, as of late, Windows has began to prefer telling users how things should be done. Not a good sign and so I left. Now I’m on Linux world. Here I can actually tell a computer how I want things done. All according to my own personal preferences. And that’s the beauty of it. Humans use computers and not the other way around.

(For more on this, watch movies about A.I. The animation genre has been particularly good at it in the past with the likes of WALL-E and Big Hero 6. And there are more recent good ones too like The Mitchells vs. The Machines and Ron’s Gone Wrong.)

Back on topic. I use keyboard shortcuts generously. I make time to learn how to make better use of my fingers. I’m on an Xfce desktop environment in Manjaro which allows me to create custom application shortcuts.

I map the Super key (usually the Windows key) to almost everything I quickly need to have on my screen. Just to name my most used ones:

Super+E = Dolphin File Manager

Alt+Space = Terminal Dropdown

Ctrl+Alt+Space = New Terminal window

Super+S = fsearch

Super+H = Task Manager

It should be said that there’s a roundabout solution on Windows with AutoHotkey. I used it myself back then and there’s a Linux equivalent called AutoKey. These are powerful keyboard automation tools and one could definitely use them to do some of these things I mention. But Windows itself doesn’t make this easy because this is third-party software and who knows what changes Microsoft decides to make in the future that could break some AutoHotkey config. It’s better to be working on a platform that doesn’t actively try to work against our best interests. Besides, there are just some things I don’t think is currently possible in Windows like the following.

wmctrl is a command that can be used to interact with an X Window manager that is compatible with the EWMH/NetWM specification. wmctrl can query the window manager for information, and it can request that certain window management actions be taken.
https://linux.die.net/man/1/wmctrl

This tool called wmctrl makes it easy to navigate between applications I already have open. I have them mapped out to a Super key combo for quick access.

The manual is detailed enough that you can practically do anything with the “windows” you have running on the system. These are the most important options to me.

1
2
3
4
5
6
7
8
9
-l        List windows managed by the window manager.
-a <WIN> Activate the window by switching to its desktop and
raising it.
-x Include WM_CLASS in the window list or
interpret <WIN> as the WM_CLASS name.
-F Modifies the behavior of the window title matching
algorithm. It will match only the full window title
instead of a substring, when this option is used.
Furthermore it makes the matching case sensitive.

There are times that an application will have multiple windows open but wmctrl will only act on the first instance it finds. It’s easy enough to address this case through a custom shell script like so. (credit for this idea to this Stackoverflow answer)

1
2
3
4
5
#!/bin/sh
#
# Activate Notes.
#
wmctrl -lix | grep 'wrapper-2.0.Wrapper-2.0' | cut -d ' ' -f 1 | xargs -i% wmctrl -i -a %

I do the same thing with Firefox which sometimes is spread out on different monitors for me.

1
2
# activate all Firefox (stable) windows
wmctrl -lix | grep ' Navigator.firefox ' | cut -d ' ' -f 1 | xargs -i% wmctrl -i -a %

And I have the path to these scripts mapped to custom keyboard shortcuts of course.

Another helpful thing about the custom application shortcuts is the ease with which I can quickly launch apps with the necessary environment variables. Like the custom DPI in this example:

1
env QT_FONT_DPI=222 exo-open --launch FileManager

The simple help these keyboard shortcuts give me is indispensable. Getting to the needed app on the screen right away decreases the risk of losing one’s focus or train of thoughts and ideas. There are probably knowledge workers who will find these keyboard-use optimizations to be a bit too much. Perhaps this just doesn’t apply to their way of working and how their brain works. To me, however, and to many others who find much relief that this feature exists on Linux, there’s no going back to old ways. Taking these out of my regular work routine would surely feel like a downgrade and will definitely have a considerable impact on everything. As one Emmert Wolf once said:

“A man is only as good as his tools.”

Tools mentioned:
wmctrl
AutoKey
AutoHotkey
Xfce

(I’m aware of tiling window managers. They just won’t do for me though because I don’t have big enough screens for them. Also, I’m not sure my brain would work just as well with too many windows available on view. Besides, I imagine it to be trickier to setup. Not to mention that many of the applications I use probably won’t play nice with the tiling thing. The far easier way of custom keyboard shortcuts easily wins for me.)