Install compizconfig-settings-manager . Search for "compiz" in the Unity Dash, or start with ccsm
from the commandline.
Warning
CompizConfig Settings Manager (ccsm
) is an advanced tool, and not all of its options are completely compatible with Unity.
As such, CCSM has been known to sometimes break users'
desktops. Please use caution and
know that you are taking a risk by following this answer or any other
answer that recommends the use of CCSM. In the event that following
this advice does break Unity, please see this
question for instructions on how
to reset it.
Find the Grid plugin:
Click on the key binding you want to change, click on Grab key combination, and press the keys you'd like to use.
On the edge of posting a duplicate of this one (but the question slightly differs), the script below does what you describe, if you run it with the arguments
python3 /path/to/script.py 2 2
However, if there are more than four windows (or more than the cells of a grid, if you use other arguments than 2 2
), the script places only the four oldest windows in the grid.
What it does
When four windows are opened, randomly placed on the screen:
running the script will arrange them in a grid:
The script
#!/usr/bin/env python3
import subprocess
import sys
#--- set your preferences below: padding between windows, margin(s)
cols = int(sys.argv[1]); rows = int(sys.argv[2]); padding = 10; left_margin = 0; top_margin = 30
#---
get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
def get_res():
xr = get("xrandr").split(); pos = xr.index("current")
return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]
def check_window(w_id):
w_type = get("xprop -id "+w_id)
if " _NET_WM_WINDOW_TYPE_NORMAL" in w_type:
return True
else:
return False
# get resolution
res = get_res()
# define (calculate) the area to divide
area_h = res[0] - left_margin; area_v = res[1] - top_margin
# create a list of calculated coordinates
x_coords = [int(left_margin+area_h/cols*n) for n in range(cols)]
y_coords = [int(top_margin+area_v/rows*n) for n in range(rows)]
coords = sum([[(cx, cy) for cx in x_coords] for cy in y_coords], [])
# calculate the corresponding window size, given the padding, margins, columns and rows
w_size = [str(int(area_h/cols - padding)), str(int(area_v/rows - padding))]
# find windows of the application, identified by their pid
wlist = [w.split()[0] for w in get("wmctrl -lp").splitlines()]
w_list = [w for w in wlist if check_window(w) == True][:cols*rows]
print(w_list)
# remove possibly maximization, move the windows
for n, w in enumerate(w_list):
data = (",").join([str(item) for item in coords[n]])+","+(",").join(w_size)
cmd1 = "wmctrl -ir "+w+" -b remove,maximized_horz"
cmd2 = "wmctrl -ir "+w+" -b remove,maximized_vert"
cmd3 = "wmctrl -ir "+w+" -e 0,"+data
for cmd in [cmd1, cmd2, cmd3]:
subprocess.Popen(["/bin/bash", "-c", cmd])
How to use
The script needs both xdotool
and wmctrl
:
sudo apt-get install xdotool wmctrl
Copy the script into an empty file, save it as twobytwo.py
Test- run it by opening four windows (at least one terminal window to run the command), the run the command:
python3 /path/to/twobytwo.py 2 2
The windows should move into a grid of 4, as in the second image
If all works fine, add it to a shortcut key: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
python3 /path/to/twobytwo.py 2 2
Note(s)
in the head section, there is a section:
left_margin = 0
I set this to zero, since gnome does not have the launcher on the left. For Unity, this should be (at least) 65
, depending on the set width of the launcher.
- As mentioned, the script, as it is, grids the four oldest windows. That means if you need more windows to be placed in the same grid, the script needs an edit. Please mention if so.
Best Answer
It is possible, as you can see from this example with four
gnome-terminal
windows:I did not manually resize and position the windows, which I could have done, but just dragged them into the corners of the screen to format them automatically. It's the same procedure as you described with placing a window into the left or right half by dragging it to the respective border of the screen. There are also custom keyboard short-cuts to achieve this.
You can set up whichever method you prefer or both.
However, the corners are by default configured the same way as the side borders and the short-cuts are probably disabled, so we have to change these settings.
Therefore we need the CompizConfig Settings Manager .
You can also install it from the terminal with the command below:
After that, launch
ccsm
.Navigate to the category Window Management and select the Grid plug-in. Go to the Corners / Edges tab and you should see the following settings:
Set all corner options according to my example above, so that when you drag a window e.g. into the upper left corner, it will get resized and placed into the top left quarter of the screen.
You can also set up keyboard short-cuts to place windows into the grid. Switch to the Bindings tab and select the short-cut key combinations you prefer. I opted for Ctrl+Alt+NumPad 1-9 to put the windows into the respective screen halves or corners. See my example below:
All your changes take effect immediately, so just close the
ccsm
window.Now you can start dragging windows into the corners: