More Windows 7 Keyboard Shortcuts for Linux

I previously blogged about getting an Aero Snap like effect in Gnome, and now I’m back for my second favorite Windows 7 keyboard shortcut: focusing a window based on its position by using WIN+Number. For example, you can focus the first window on the taskbar by pressing Win+1, the second one with Win+2, etc

To accomplish this, I wrote a simple script that accepts one parameter: the number of the window to focus. For example, to focus the first window on the current workspace, call ‘./script.sh 1′. As a bonus, I added my previous Aero Snap code – you can invoke it by passing either ‘left’ or ‘right’ as a parameter to the script. You can install the script by running the following:

gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_1 "<Super>Left"
gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_2 "<Super>Right"
gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_3 "<Super>1"
gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_4 "<Super>2"
gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_5 "<Super>3"
gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_6 "<Super>4"
gconftool-2 --type string --set /apps/metacity/global_keybindings/run_command_7 "<Super>5"

gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_1 "/bin/bash /path/to/script.sh left"
gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_2 "/bin/bash /path/to/script.sh right"
gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_3 "/bin/bash /path/to/script.sh 1"
gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_4 "/bin/bash /path/to/script.sh 2"
gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_5 "/bin/bash /path/to/script.sh 3"
gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_6 "/bin/bash /path/to/script.sh 4"
gconftool-2 --type string --set /apps/metacity/keybinding_commands/command_7 "/bin/bash /path/to/script.sh 5"

Note that you may need to configure the script a little bit depending on the number of panels you have; there is a variable (numIgnore) at the top of the script that defines how many sticky windows to skip. If you open a terminal and run ‘wmctrl -l’, you will see that the first couple of windows listed are your panels (‘Bottom Panel’ and ‘x-nautilus-deskop’ for me). You don’t want to focus those with Win+1 and Win+2, so my script lets you define how many windows to skip over. Also note that some windows do not play nice – Empathy’s Contact List, for example, will always be listed on a workspace whether it is open or not.

Here is the script. Remember that you need wmctrl installed to be able to use it:

# Number of sticky windows to ignore (panels)
numIgnore=2

# Activate Nth window on workspace
if [ $1 = `echo "$1" | tr -d "[:alpha:]"` ]; then
	curdesk=`wmctrl -d | awk '/[0-9]+ +\*/ {print $1}'`
	winnum="$numIgnore"
	winnum=$((winnum + $1))
	wmctrl -i -a `wmctrl -l | egrep "[0-9a-fx]+\s+$curdesk|-1" | cut -f 1 -d ' ' | head "-$winnum" | tail -1`
fi


# Resize active window to half width, 100% height (on left side of screen)
if [ "$1" = "left" ]; then
	wmctrl -r :ACTIVE: -e 0,0,0,`xwininfo -root | grep Width | awk '{ print (($2/2)-6)}'`,`xwininfo -root | grep Height | awk '{ print $2 }'`
fi

# Resize active window to half width, 100% height (on right side of screen)
if [ "$1" = "right" ]; then
	wmctrl -r :ACTIVE: -e 0,`xwininfo -root | grep Width | awk '{ print (($2/2)+5) ",0," (($2/2)-6) }'`,`xwininfo -root | grep Height | awk '{ print $2 }'`
fi

Leave a Comment