How to use Window snapping for Ubuntu? [duplicate]

This is easy to enable from the CompizConfig Settings Manager. (Install it, if you haven't already).

Then under Window Management, enable Grid. I've kept the default bindings of CTRL+ALT+NumPad but you can easily change it Super + Arrow as you prefer.


Go here for full instructions in a human readable form from Mike's Ubuntu, (thank you Mike) and here is the original Ubuntu forum post (thanks gotsanity). I've checked this on my system so it should work for you. I've also added and edited a little to try to make it a bit easier to understand.

The only changes I would make to his directions are that you first check to see if you have compiz installed (easiest way is to use the software manager and put "compiz" in the search window.

If you do then type the following in a terminal:-

sudo apt-get install compizconfig-settings-manager wmctrl

Otherwise the compiz settings manager won't be present on your system.

Open a terminal and type:

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 10. Yours will very likely be different.
These next commands create a new hidden folder in your home folder to hold the scripts needed and moves you into it.

mkdir .scripts

cd .scripts

Then type

gedit compizsnap-left.sh    

A text editor will pop up. Paste this in, and then change your mouse id from 11 to the number from the output of the "xinput list" command. When you have done this, save the file.

#!/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. (Not forgetting to change the mouse ID)

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    

And

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    

(Did you remember to change the mouse ID)

Now we need to tell compiz how to behave when the mouse moves to the right, top and left of the screen.

Run System > Preferences > CompizConfig Settings Manager. Go to the command section. Set the following:
In the Command 0 field:

sh ~/.scripts/compizsnap-left.sh

In the Command 1 field:

sh ~/.scripts/compizsnap-right.sh

In the Command 2 field:

sh ~/.scripts/compizsnap-max.sh

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

N.B. Compiz may complain that there are already functions set to the edge bindings you want to use - replace them with yours.


One of the differences between most Unix window managers and Windows is that most Unix window managers offer resistance to the movement of windows at the edges of the screen. Therefore you don't need any special commands to move windows to the left or right edge of the screen; just drag one toward an edge and it will stop when it gets there. I don't know of any Gnome (the standard Ubuntu window manager for now) keyboard shortcuts that move windows.