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
#!/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
#!/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
#!/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!

Nuts. It didn't work. I'm a "newbie" at this so I probably need to look for extremely detailed directions. Thanks though! Good practice for me working in the Terrifying Terminal.
did you run the first command to get compiz? It didn't work for me at first, I already had compiz, so I didn't do the first command. So I done everything else, and it didn't work. So I ran the first command, and the only thing that it installed was "wmctrl", so your probably missing "wmctrl". So just run sudo apt-get install compizconfig-settings-manager wmctrl, it will say only wmctrl needs to be installed, then it should work, like mine.
Yep, did not work here either. Not sure is it has something to do with another compiz setting
did you check that you changed the mouse ID correctly (for all 3 scripts)? I set the values for my laptop touchpad, and it works there, but when I plug in a USB mouse it doesn't work. (as I would expect)
Why is mike's MOUSE = "11", is it zero-indexed…I have id = 11, so should I put MOUSE = "10"?
did you run the first command to get compiz? It didn't work for me at first, I already had compiz, so I didn't do the first command. So I done everything else, and it didn't work. So I ran the first command, and the only thing that it installed was "wmctrl", so your probably missing "wmctrl". So just run sudo apt-get install compizconfig-settings-manager wmctrl, it will say only wmctrl needs to be installed, then it should work, like mine.
i dont think grid plugin is exactly the same since you have to press certain keys to get it done.
this snap windows to side works jus by dragging a windows to side of screen.
btw i tried this. works fine
thanks mike
i post a video of it in action. check it out
http://www.youtube.com/watch?v=WywdLQAoZ5g
Another noob here. I really need help trying to use ubuntu 10.04.It works great off the live cd.I can download my broadcom drivers and get online,so I installed ubuntu over windows. Everything BUT my wireless works and I can't download the drivers .Besides hardwiring my laptop to go online, is there anything else I can do?
Sorry about being off subject, mybad.
Do you know how to import
the effect that is seen each time a window is open/closed in windows 7?
I'm vietnamese very excited to be familiar with you and I like to learn many things from the you.
Thanks! Great tutorial! It’s very useful on my enormous wide-screen monitor.
One mistake…what is “cd mkdir .scripts cd .scripts”? It was pretty obvious to me what this was supposed to do, but it could be giving some Linux-inexpierenced people problems. So for them…
Should be (from your home directory, which is where you’ll probably be by default when you open your terminal):
mkdir .scripts
(This creates a folder called “.scripts”…same as opening your file manager, right-clicking, and clicking on “Create New Folder”, and naming the new folder “.scripts”. Starting a folder or file name with a dot (.) makes it “hidden”. Then…)
cd ./.scripts
(“cd” to change directory, just like clicking on a folder in your file manager. But first you’d have to press CTR H, or click on “Show Hidden Files” in the “View” menu, or you won’t see it, since it starts with a “.” and is therefore hidden. “./” is for whatever directory you’re currently in, so if you’re in /home/Myusername, typing “./.scripts” is the same as typing “/home/Myusername/.scripts”.)
If you’re having trouble, just make sure you followed the steps correctly…make sure you have the necessary packages (software) installed, make sure the commands did what they were supposed to…
I was very confused when the right snap would work, but not the left. Until I opened the script file and saw that it was a blank page. Guess I forget to save it. Oops! The same thing would happen if missed a key a misspelled the file name somewhere along the line, or something like that.
Hi thanks Great work.
but only the left side snap not working plz help
Change the following in the left sh file
if [ "$(/usr/bin/X11/xinput --query-state $MOUSE | grep "valuator[0]=." | sed s/"valuator[0]="//)" -le 10 ]
to:
if [ "$(/usr/bin/X11/xinput --query-state $MOUSE | grep "valuator[0]=." | sed s/"valuator[0]="//)" -ge $TEMPWIDTH ]
Using mouse ID for touchpad right snap worked but not top and left. Using mouse ID for USB mouse right and top snap worked but left still failed. This change didn't help in either case. Initially I thought the left or top panel might interfere with the script, however moving the left panel to right didn't alter the situation. It seems like more people have problem with left snap. Will you be looking into this more carefully?
In anycase, I enjoy right and top snaps. Many thanks!
One small step for man, one giant leap for Ubuntu…
Great step-by-step tutorial. It works perfectly for me (Ubuntu 10.10 )
[...] 7-like snapping windows to half/full screen size: http://www.mikesubuntu.com/2010/06/snap-windows-to-sides-like-windows-7-using-compiz/I have tried this on Ubuntu 10.10, and it works beautifully. GA_googleAddAttr("AdOpt", "1"); [...]
worked great for me on 10.10 except since I have two monitors the snap takes up more than half of the screen. Trying to find the documentation I need to edit the scripts to fix this. Snap to top works perfect with two monitors though!
Same here. Two monitors. What options do we have?
I'm having the problem with dual monitors also.
Is there a way to set multiple values for the mouse ID in the scripts? I'm on a laptop, so I have the track pad, plus a USB mouse and a graphics tablet. I have it set to the mouse because I use that most often, but it'd be nice if it worked for all three.
The snap on the right side works fine. But the max and left side wont work. I am using Ubuntu 10.10 on an Acer Aspire 5534.
Thanks in advance.
I tried doing this on Fedora 14 x84_64. Only notable change was that when I installed xinput, it didn't place it in /usr/bin/X11/xinput. So, I opened each script with gedit and replaced all instances of "/usr/bin/X11/xinput" with just "xinput". It works great now, thanks a lot!
[...] http://www.mikesubuntu.com/2010/06/snap-windows-to-sides-like-windows-7-using-compiz/ [...]
[...] http://www.mikesubuntu.com/2010/06/snap-windows-to-sides-like-windows-7-using-compiz/ [...]
Class! Like a boss! Simply worked perfectly. Thank you!