Custom window management in Ubuntu/Unity/Compiz

Someone asked on the Ubuntu users mailing list how they could set up a keypress that would resize the currently active window to 75% of its present width. It took a little thought, but eventually the tools were found…

The tools we need are wmctrl and xdotool. Depending on how you like to do maths in scripts, you may need to install the appropriate calculator; I used calc:

sudo apt-get install xdotool wmctrl calc

You will also need the Compiz configuration settings manager:

sudo apt-get install compizconfig-settings-manager

First you need to be able to identify the window to operate on. You can do this via its title, but using its unique window ID number is better. The window ID is usually shorter and it is unique across all windows even if several windows have the exact same title.

This command will retrieve the window ID of the currently active window (i.e. the window that has the focus) and put it into a variable called WIN:

WIN=$(xdotool getactivewindow)

We can also use xdotool to retrieve a window’s geometry. By default, the results are displayed for human consumption:

kauer@kt:~$ xdotool getwindowgeometry $WIN
Window 102763394
  Position: 1028,526 (screen: 0)
  Geometry: 826x554

If we include the “--shell” option, the results are helpfully returned in a form suitable for evaluating in a shell:

kauer@kt:~$ xdotool getwindowgeometry --shell $WIN

The following command will result in shell variables WINDOW, X, Y, WIDTH, HEIGHT and SCREEN:

eval $(xdotool getwindowgeometry --shell $WIN)

Now we need to figure out a width that is 75% of the current width. I’ve used calc:

WIDTH=$(calc "int($WIDTH*0.75)")

The next bit is needed because setting the window dimensions is ignored when a window is being displayed maximised. This command un-maximises our chosen window:

wmctrl -i -r $WIN -b remove,maximized_vert,maximized_horz

Thanks to Ralf Mardorf for that trick 🙂

Now we can finally set the new window width that we calculated above. The five parameters to the -e option are gravity, x position, y position, width and height. Gravity is usually left at zero, meaning “default”. For the other four, passing a value of “-1” means “leave this value unchanged”. Notice how we use WIN again to specify the window; we could also use WINDOW:

wmctrl -i -r $WIN -e "0,-1,-1,$WIDTH,-1"

Putting it all together, we get a script (let’s call it ““):

WIN=$(xdotool getactivewindow)
eval $(xdotool getwindowgeometry --shell $WIN)
WIDTH=$(calc "int($WIDTH*0.75)")
wmctrl -i -r $WIN -b remove,maximized_vert,maximized_horz
wmctrl -i -r $WIN -e "0,-1,-1,$WIDTH,-1"

We now need to bind this script to a key combination. We can’t just run this script in a terminal window, because it would just resize the terminal window! We need it to run on whatever window we have currently selected, and for that we need to bind the script to a key combination that we can press in any window.

To do this we use the “Commands” module in Compiz. Start the Compiz configuration settings manager, then:

  • Click on “General” in the left hand pane
  • Check the box beside the “Commands” module if it is not already checked
  • Click the “Commands” button to open the “Commands” module
image of the General tab in the Compiz configuration settings manager, showing the Commands module enabled

Enable the Commands module in the General tab of the Compiz configuration settings manager

  • In the Commands module, click on the “Commands” tab and enter the full path and name of the new script into one of the command files – for example “Command line 0”.
Image showing a runnable command being entered into a slot on the Commands tab of the Commands module of the Compiz configuration settings manager

Setting up a runnable command in the Commands module of the Compiz configuration settings manager

  • Click on the “Key Bindings” tab, and click the “Disabled” button for the command line you chose. You will be prompted to enable the command by checking an “Enable” checkbox.
  • Checking the “Enable” checkbox will cause the dialogue to prompt you for a key combination. The simplest way to choose a key combination is to click on the button labelled “Grab key combination”, press the key combination you want, then click OK.
Image of the dialogue box where a key combination may be entered or grabbed in the Commands module of the Compiz configuration settings manager

After enabling the key binding for a command, you will be prompted to select a key combination

  • Finally close the Compiz settings manager.

If you now select any open window, then press your chosen key combination, the window will shrink horizontally to 75% of the its previous size.

I highly recommend reading the wmctrl and xdotool man pages thoroughly – you will get lots of interesting ideas!

Leave a Reply

Your email address will not be published. Required fields are marked *