You are here: Home » Ubuntu » Create keyboard shortcuts to move/resize windows in Ubuntu

Create keyboard shortcuts to move/resize windows in Ubuntu

2 votes

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.

Asked by Herms on June 16, 2010.

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 on June 16, 2010

Opening a window at a particular size is rather easy. The trick is being able to modify pre-existing windows. - Herms on June 16, 2010

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 on June 16, 2010

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 on June 16, 2010

@Herms 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 on June 17, 2010

Accepted Answer1 votes

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.

Answered by Florian Diesch on June 16, 2010. Last Edited on June 16, 2010.

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 on June 16, 2010

And 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 on June 16, 2010

+1 for answer, +1 to request for explanation ;) - Andy on June 17, 2010

"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 on June 17, 2010

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 on June 17, 2010

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 on June 17, 2010

Content from Superuser of Stack Exchange. Original article at Superuser.


Related Tags

Ubuntu 3445
Gnome 350
Autohotkey 111
Scripting 70