wmctrl -- Shade, move, resize windows from a shell

by Kyle Rankin

Related link: http://sweb.cz/tripie/utils/wmctrl/



From the "why didn't I know about this before now?" file: this evening I discovered the wmctrl tool. With this you can access all of the Extended Window Manager Hints functions of your windows from the command line. What does this mean? Imagine hitting a key sequence and a specific window moves, and resizes to a certain section of your desktop. Hit another button and it shades, no matter what window has focus. I'll get into how to unlock all of this potential below.

Now, being a user of the enlightenment window manager for some time, I have been accustomed to being able to script window events through their included eesh tool. The downside to eesh is that it only works for enlightenment. This sort of feature was one of the main things keeping me using enlightenment apart from fine-grained window memory. Since fluxbox recently gained similar window memory functionality all that was missing was eesh--until today. The wmctrl tool works with any window manager that supports Extended Window Manager Hints which means that it will work with icewm, kwin, metacity, openbox, fluxbox, enlightenment, sawfish, xfce, and a ton of other window managers.

To illustate why this is a cool program, I will show you how to use it to create a "quake console." If you have ever played doom or quake, you are familiar with the little command console that shows up whenever you press the ~ key. I wanted to have similar functionality on my desktop so that when I hit alt-~ a terminal would pop down to type in, but otherwise it would disappear out of my way. To do this you need to hide a window up at the top of your screen in a shaded state, and then have it unshade whenever you hit ~ (or alt-~ so you can type ~ inside the terminal).

First, download wmctrl from http://sweb.cz/tripie/utils/wmctrl/, unpack it, and build it with the standard ./configure && make && make install that you might use for building any other Linux program from source.

Once wmctrl is installed, type wmctrl -l to get a list of open windows. It will look something like this:

greenfly@boxer:~$ wmctrl -l
0x00600003 0 boxer gkrellm
0x01800047 0 boxer quake_console
0x01000072 0 boxer Mozilla Firefox
0x01600012 0 boxer irssi_term
...


The first column is a unique ID number assigned to each window, the second column specifies which desktop the program is on, the third column tells you which machine the window is running from, and the fourth column tells you the all-important title of the window.

If you want to create a quake console, open a terminal and use command-line arguments to give it a unique title. This argument varies depending on your terminal, but if you used xterm you would type something like xterm -title "quake_console". As you can see, I already have a window in my window list with that title. Now move this window to the top lefthand corner of your screen, and if you can, move it so that the titlebar is actually moved off the screen and out of sight, and only the terminal itself is showing (this way when it shades it will totally disappear from view).

Now, to shade this window, all you need to do is open up a different terminal and run wmctrl -r "quake_console" -b toggle,shaded. The window should shade out of sight. Run the command again and the window will come back into view. Now you can set this command to a keybinding of your choice like alt-~ and have a true quake console always at the ready.

This is only the beginning of the possibilities a program like this presents. Say you usually like your web browser to fill up your desktop, except for when you are IMing with friends. In those cases you resize it to make room for both windows. With this program you could set up all of those resizing and movement functions inside a script and bind it to a key sequence. Want to browse and IM? Hit a key sequence and all the windows move and resize. You can then hit another key sequence and be back where you started. If you wanted, you could even write a script that made a window bounce across the screen.

The sorts of things you can do with wmctrl are mostly limited by your own scripting ability. What's nice about it is that once you have your scripts set up, you can move from one window manager to the next and your scripts will still work. Want to switch from KDE to Gnome? All you have to do is reset your key bindings and wmctrl will still work the same.