Wednesday, May 18, 2016

Linux window shading with wmctrl (toggle all windows script)

Wmctrl shade toggle script for Linux

Linux Windows Management

If you use Linux then you may be familiar with and/or love the fact that it has the most amazing and arguably best window management systems compared to Windows or Mac (both of which you only have one choice). In Linux there are various WM (Window Management) systems you can use and most can be used in lots of Linux distributions (Debian, Mint, Opensuse, etc...) depending on the WM.

One nice feature of some WM's is the shading/rollup feature, which instead of minimizing a window you can just hit the shade button and it will be rolled up to a just a thin title bar allowing you to see more of your desktop and/or other windows, but being reminded that window is open and you have stuff left to do there. This feature usually works well in most distros, but not always and not always well in every application type. In Ubuntu which is one of the biggest distros this ability has been ruined by the unity compiz plugin and so any window shading will now just minimize the window. This is, of course, if you use Unity as your WM.


If you do have issues with normal window shading buttons (Opensuse has some issues) or normal hotkeys not executing shading then you can almost always write a simple shell script using wmctrl:

            wmctrl -r :ACTIVE: -b toggle,shaded

If you call this simple script using a hotkey this will shade whatever window is is currently active. You can also toggle the shade state of a specific window. By using wmctrl -l you can list all active windows and get the names and ids of each. Using the window name you can then do this:

wmctrl -l # <- lists all windows

    # E.g:
    #0x03600017 -1         N/A Desktop — Plasma
    #0x0360001a -1         N/A Plasma
    #0x03600025 -1         N/A Plasma
    #0x01800001  1 fluxinferno Blogger: The Agnostic Microcosm - Edit post - Google Chrome
    #0x0500002b -1 fluxinferno Guake!
    # then with the window title you can toggle its' shaded state with:

wmctrl -r "Google Chrome" -b toggle,shaded
WMCTRL comes already pre-installed in most distros, but I am pretty sure it can be found for almost any distro you can imagine if not.

Wmctrl to toggle shade state of all windows

Sometimes you may just want to toggle the shade states of all windows. That gets a little tricker as you have to list all windows currently running, store the window names and and run the above command on each name. This can be done using a for loop on the list command. The below code can be run from your terminal, but is better used in a script and set to run with a hotkey. I usually use Alt+X to toggle active and Ctrl+Alt+X to toggle all. Check out this shell script to toggle shade on all windows:

for i in $(wmctrl -l | awk '{print $4}'); do
  wmctrl -r "$i" -b toggle,shaded

Just save the above script to a file and make sure it's executable and place it in any folder your shells PATH is read from. Then set a hotkey to the script and execute as needed.

No comments: