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:
sudo apt-get install compizconfig-settings-manager
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:
The method in drizzle's answer will work, but has a few downsides
Alternative option
You could simply do with the command:
wmctrl -a chrome
which will raise the first found window with "chrome" in its name. That would however also raise the window I am writing in now (a firefox
window, named: "Shortcut to open current Google Chrome...")
The use of the window's pid will prevent this. In my opinion, it is therefore more elegant to put the command:
wmctrl -ia "$(wmctrl -lp | grep "$(pgrep chrome)" | tail -1 | awk '{ print $1 }')"
under a shortcut key (Alt+1 will work), given the fact that you'd need to have wmctrl
installed then:
sudo apt-get install wmctrl
How to add the command to a shortcut
Install wmctrl
sudo apt-get install wmctrl
create a small script (open an empty file, paste the script below in it)
#!/bin/bash
wmctrl -ia "$(wmctrl -lp | grep "$(pgrep chrome)" | tail -1 | awk '{ print $1 }')"
Save it as raise_chrome.sh
Add the script to a shortcut; choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
/bin/bash /path/to/raise_chrome.sh
to (e.g.) the shortcut Alt+1.
N.B. Use an absolute path, not e.g. ~
Explanation
The command is in fact a "conversion" of this one, and the explanation is similar:
In the section:
"$(wmctrl -lp | grep "$(pgrep chrome)" | tail -1 | awk '{ print $1 }')"
the command:
wmctrl -lp
lists all windows, including their pid and window- id. The result is piped to
grep "$(pgrep chrome)"
which filters (lists) all occurrences of Chrome
windows.
Then,
tail -1
will give us the last item in the list, which is the latest created window (since wmctrl
lists windows chronologically).
awk '{ print $1 }'
finally, will give us the first string in the line (from the output of wmctrl
), which is the window- id.
The command:
wmctrl -ia <window_id>
Will then raise the last created window of Chrome
Note
No need to say that you can replace chrome
in the command by any other application (process name), and place them under separate key combinations.
EDIT
use the application as an argument
Since you mention Sublime Text
, Terminal
and Chrome
, you might want to use multiple shortcuts, and run the script with the application as an argument.
To do so
Use the script with slight difference:
#!/bin/bash
app=$1
wmctrl -ia "$(wmctrl -lp | grep "$(pgrep "$app")" | tail -1 | awk '{ print $1 }')"
and run it subsequently with the commands under the shortcut keys:
sh /home/jacob/Bureaublad/raise.sh sublime_text
sh /home/jacob/Bureaublad/raise.sh gnome-terminal
and
sh /home/jacob/Bureaublad/raise.sh chrome
Best Answer
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
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
How to use
The script needs both
xdotool
andwmctrl
: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:
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:
Note(s)
in the head section, there is a section:
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.