Linux Problem: wmctrl, Documentation

By Xah Lee. Date:

unix/linux has one hundred annoyances. Here's another example.

I discovered “wmctrl” tool. It lets you switch windows programmatically. So, you can assign a key such as F9 to run the command wmctrl -a emacs, and it'll switch you to emacs. This is specifically mentioned in its manual, quote:

Going to the window with a name containing 'emacs' in it
    wmctrl -a emacs

So, i was extremely delighted. I set my keys, tested that it works. However, the next day, i noticed that it switched me to Firefox instead. Then, i realized that's because i have a page in Firefox that's a article about emacs, so the page's title contain “emacs”. Then, also realized, that by default, GNU emacs changes its window title by file/buffer name.

So, this means, the example in the manual is practically useless!

The bottom line is, then, you start to hack for a solution. One hour, 2 hours, a whole day? How can something blatantly doesn't work yet mentioned in the manual as a example? Is the developer aware that the example he gave is practically useless?

Solutions

use the “-x” flag.

-x   Include WM_CLASS in the window list or interpret <WIN> as the WM_CLASS name.

Like this: wmctrl -xa emacs

You can find out class of the window with xprop utility. Type it in terminal, then click a window, then result will be printed in terminal. Look for the line “WM_CLASS”.

Alternatively, use xdotool. Bind a key to run the following:

#!/bin/sh
xdotool search --all --class --onlyvisible --limit 1 "$1" windowactivate || eval $2

2012-09-15 Thanks to {Yuri Khan, Vadim Atlygin, Evan Cofsky}.