Posted by & filed under aero, compiz, ubuntu, windows 7, windows vista.

My computer has a big screen, and the Aero window-snapping feature works beautifully. With it, you can drag a window to the edge of the screen, and Windows will then automatically resize the window to half the width of the screen, positioned on the side. This is great for window management. Today I was looking for a way to do this in Ubuntu using Compiz and I found one in a forum post on Ubuntuforums.org.

Let’s do it:

First, you will need the compiz settings manager.

sudo apt-get install compizconfig-settings-manager wmctrl

Then, run:

xinput list

It will display a list. Find your id number for your mouse. You will need this number in the next steps. Mine was 12. Yours will very likely be different. This command creates a new hidden folder in your home folder to hold the scripts needed.

mkdir .scripts

cd .scripts

Then, run

gedit compizsnap-left.sh

A text editor will pop up. Paste this in, and then change your mouse id to the number from above. When you have done this, save the file.

compizsnap-left.sh

Code:
#!/bin/sh
#
# CompizSnap is a collaborative project from ubuntuforums.org and is free software.
# This script adds window snapping functionality to compiz using the commands plugin.
#
# Directions: run "xinput list" to find your mouse's ID# and then edit the MOUSE variable below:
#

MOUSE="11"

# ----- Don't edit below this line unless you know what you are doing.
WIDTH=`xdpyinfo | grep 'dimensions:' | cut -f 2 -d ':' | cut -f 1 -d 'x'` && HALF=$(($WIDTH/2-10))

echo $WIDTH
TEMPWIDTH=$(($WIDTH-10))
echo $TEMPWIDTH
if /usr/bin/X11/xinput --query-state $MOUSE | grep down
then
   	while (/usr/bin/X11/xinput --query-state $MOUSE | grep down)
	do
		echo 'button pressed'
	done

	if [ "$(/usr/bin/X11/xinput --query-state $MOUSE | grep "valuator\[0\]=." | sed s/"valuator\[0\]="//)" -le 10 ]
	then

		wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz && wmctrl -r :ACTIVE: -b add,maximized_vert && wmctrl -r :ACTIVE: -e 0,0,0,$HALF,-10

	else
		echo "exiting without matching"
		exit 1
	fi
else
		echo "exiting because button isnt "
        exit 1
fi

Then, do the same for the other two scripts.

compizsnap-right.sh

Code:
#!/bin/sh
#
# CompizSnap is a collaborative project from ubuntuforums.org and is free software.
# This script adds window snapping functionality to compiz using the commands plugin.
#
# Directions: run "xinput list" to find your mouse's ID# and then edit the MOUSE variable below:
#

MOUSE="11"

# ----- Don't edit below this line unless you know what you are doing.
WIDTH=`xdpyinfo | grep 'dimensions:' | cut -f 2 -d ':' | cut -f 1 -d 'x'` && HALF=$(($WIDTH/2))

echo $WIDTH
TEMPWIDTH=$(($WIDTH-10))
echo $TEMPWIDTH
if /usr/bin/X11/xinput --query-state $MOUSE | grep down
then
   	while (/usr/bin/X11/xinput --query-state $MOUSE | grep down)
	do
		echo 'button pressed'
	done

	if [ "$(/usr/bin/X11/xinput --query-state $MOUSE | grep "valuator\[0\]=." | sed s/"valuator\[0\]="//)" -ge $TEMPWIDTH ]
	then

		wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz && wmctrl -r :ACTIVE: -b add,maximized_vert && wmctrl -r :ACTIVE: -e 0,$HALF,0,$HALF,-1

	else
		echo "exiting without matching"
		exit 1
	fi
else
		echo "exiting because button isnt "
        exit 1
fi

compizsnap-max.sh

Code:
#!/bin/sh
#
# CompizSnap is a collaborative project from ubuntuforums.org and is free software.
# This script adds window snapping functionality to compiz using the commands plugin.
#
# Directions: run "xinput list" to find your mouse's ID# and then edit the MOUSE variable below:
#

MOUSE="11"

# ----- Don't edit below this line unless you know what you are doing.
if /usr/bin/X11/xinput --query-state $MOUSE | grep down
then
   	while (/usr/bin/X11/xinput --query-state $MOUSE | grep down)
	do
		echo 'button pressed'
	done
	if [ "$(/usr/bin/X11/xinput --query-state $MOUSE | grep "valuator\[1\]=." | sed s/"valuator\[1\]="//)" -le 10 ]
	then

		wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz

	else
		echo "exiting without matching"
		exit 1
	fi
else
		echo "exiting because button isnt "
        exit 1
fi

Now run System > Preferences > CompizConfig Settings Manager. Go to the command section. Set the following:

Command 0:

sh ~/.scripts/compizsnap-left.sh

Command 1:

sh ~/.scripts/compizsnap-right.sh

Command 2:

sh ~/.scripts/compizsnap-max.sh

Finally, go to the edge bindings tab. Set command 0 to left, set command 1 to right, and set command 2 to top.

Now you should be able to automatically resize your windows. Dragging and dropping a window to the top of the screen will maximize the window, the right side will make it 50% of the screen width positioned on the right, and the left side will do the same on the left. Note that there is no graphical indication that shows you that you are about to resize the window, like there is in Aero.

Enjoy!