Create keyboard shortcuts to move/resize windows in Ubuntu

3

3

On windows I have a few AutoHotkey scripts that let me hit various key combinations to resize windows to particular pre-defined sizes, or move them to certain areas of the screen (or both). I'm wondering how I would accomplish this in Ubuntu (gnome).

Most of the searches I did for a linux version of autohotkey seemed to be centered around just setting up simple keyboard shortcuts (key combo X launches app Y, etc). I didn't see anything about scripting window size and placement beyond maximize/minimize.

Herms

Posted 2010-06-16T13:10:06.083

Reputation: 7 644

Won't add it as an answer cos it's too vague I'm afraid - I use to have Compiz installed and I used it to open up a terminal window at a preset size when I pressed a shortcut key. I know this isn't exactly what you want, but try looking under Compiz - there might be something there. – Andy – 2010-06-16T13:34:35.550

Opening a window at a particular size is rather easy. The trick is being able to modify pre-existing windows. – Herms – 2010-06-16T13:54:05.293

Minimize and Maximize are usually built into your window mananger, not sure about move windows -- I had a similar question for Xubuntu (http://superuser.com/questions/142195/xubuntu-aero-snap-hotkeys)

– wag2639 – 2010-06-16T17:13:33.727

min and max aren't what I want. I'm looking for something where I can set specific sizes. For instance, one thing I had in windows was to set the size to 90% of the current screen. – Herms – 2010-06-16T18:06:59.480

I know that opening at a particular size is easy, and I know it's not what you're after... it's just my vague recollection of setting up my terminal make me think there might be something useful for you there. If I still used it I'd check it out for you! – Andy – 2010-06-17T09:16:16.347

Answers

2

In shell scripts you can use wmctrl (not installed by default in Ubuntu, but you can install it using the package manager) to move and resize windows.

The following script resizes the current window to 90% of the screen width and height:

#!/bin/sh

set -- $(xwininfo -root| awk -F '[ :]+' '/ (Width|Height):/ { print $3 }')
width=$1
height=$2

wmctrl -r :ACTIVE: -e 0,-1,-1,$((width*90/100)),$((height*90/100))

This should work in Xubuntu, too.

Florian Diesch

Posted 2010-06-16T13:10:06.083

Reputation: 3 380

Looks like that gives the total width/height of the entire logical screen, not just a single monitor. Any way to get the size screen area that's specific just to one of the monitors? – Herms – 2010-06-16T18:38:48.457

1And if you have a chance and wouldn't mind, could you give a brief explanation of what each part of that is for (mainly the args to awk)? Will make it easier to customize that. – Herms – 2010-06-16T18:42:49.720

"xwininfo -root" prints some information about the root window. awk takes its output, selects the lines containing either " Width:" or " Height:" and prints the values.

Usally "awk '/ (Width|Height):/ { print $2 }'" works here, too, I just needed the longer one to work around a bug some time ago and got used to it.

The two values are then used as arguments to "set --" which sets $1 and $2 to that values. For better readability I want to use $width and $height instead so I set them accordingly.

In the wmctrl call this variables are then used to calculate the new window width and height. – Florian Diesch – 2010-06-17T13:52:38.283

I don't know how to get the size of the monitor a window is on if your screen is stretched on multiple monitors and I don't have a second monitor around to try a bit. Maybe one could use xrandr to get the resolutions and then use the screen position to find out which monitor it is. Getting this right with all the options for turning, reflecting, scaling, ... and all possible relative monitor positions may get a bit tricky. Maybe it's worth a question of its own... – Florian Diesch – 2010-06-17T14:09:38.383

Thanks for the extra info. I noticed a little weirdness with the Y coordinates (wmctrl's Y=0 is right below the ubuntu menu bar thingy, while xwininfo's Y=0 is the top of the screen), but I should be able to work around that. This still isn't quite as nice as autohotkey, but that's probably expected given the WM fragmentation. – Herms – 2010-06-17T14:15:36.407

0

Just to approve upon @Florian's answer...

#!/bin/bash

$(xwininfo -root | awk -F ': +' '/ (Width|Height):/ { print $2 }')
width=$1
height=$2

wmctrl -r :ACTIVE: -e 0,-1,-1,$((width*90/100)),$((height*90/100))

Why is this better?

The change from [ :]+ as the field seperator, to : + Makes it require a colon, then a space, in that order, not just one or more of either.

This means that it no longer breaks when you want to use things like Absolute upper-left, that have spaces in them. In @Florian's answer, awk'ing for Absolute upper-left X: 123 would result in upper-left, not in 123.

To futher continue on how to expand this better; you could take the example above and do something like...

set -- $(xwininfo -root | awk -F ': +' '/ (Absolute upper-left X|Absolute upper-left Y|Width|Height):/ { print $2 }')
posX=$1
posY=$2
width=$3
height=$4

wmctrl -r :ACTIVE: -e 0,$((posX+someNumX)),$((posY+someNumY)),$((width+someNumW)),$((height+someNumH))

to move it by <someNumX, someNumY> and resize it by <someNumW, someNumH>.

KingTide44

Posted 2010-06-16T13:10:06.083

Reputation: 1