I have 3 monitors on my development machine, which runs Ubuntu + Unity. I usually dedicate one monitor to web browsing and IM, and the other two to terminals where I run vim or enter commands. I'm looking for a single-key-sequence to switch between them at-will.
I would like to know how I can switch focus to (top-) windows on each monitor. The behavior I'm looking or is sort-of like ALT+TAB, except instead of rotating between applications (which only makes the most-recently-used instance of application available in the rotation), I can rotate focus between monitors.
As a compromise, I could stand to use the ALT+TAB mechanism if I could have each and every window in the list. I still imagine this getting annoying, though.
Best Answer
Script(s) to rotate focus through monitors
In the setup below, two scripts are involved: one background script to keep track of the history of focussed windows (see the explanation at the bottom to read why that is needed), and one "action" script to place under a shortcut key, to set focus on the next screen. If the next screen currently has no window to set focus on, a message is displayed:
The scripts
Script 1; the background script, save it (exactly) as
focus_track.py
Script 2; the action script, save it as
next_focus.py
In one and the same directory as script 1.How to use
The scripts needs
wmctrl
to be installedfocus_track.py
. The name is important, since both scripts share functions; script 1 is imported into script 2.next_focus.py
in one and the same directory as script 1.Test- run the setup: N.B. Start the background script before opening (and thus focussing) windows. Windows, opened before the background script starts are not "recorded" untill they are focussed
Start the background script (from e.g. a terminal) with the command:
On your different screens, open windows.
Run script 2 with the command:
The focus should switch to the next screen. If the current screen is the last in the row, the focus switches to the first screen.
If all works fine, add script 1 to Startup Applications: Dash > Startup Applications > Add the command:
and add script 2 to a keyboard shortcut: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
to a shortcut key of your liking.
Notes
Explanation:
What it takes
To switch focus from one screen to another, it is necessary to determine which is the front most window per screen. The major issue is however that windows, spread over multiple screens are actually all in one and the same stack, and thus ordered in one and the same succession (z-wise). The tools we have (
wmctrl
,xdotool
,xprop
etc.) in the best case can only determine the currently active window. They give us no information whatsoever about the window order on the other screens, since the windows are below the active window.Therefore, at first sight, it seems pretty impossible to switch focus from one screen to another.
However:
How to get the information
With a workaround however, there is an escape: if we make a background script keep track of the currently focussed window, and maintain a history record of changes (for as long as the window exists), we actually can conclude what is the z-order of the currently opened windows. If we also keep track of their geometry and position, we have all information we need.
An example:
We have currently five windows: A, B, C, D, E. If their focus changes through D, E, A, C, B, we know the z-order of the windows is: B, C, A, E, D (front to back)
Together with their positions (x-wise) and the screen data (the x-resolution of the screens) we have all information we need. To switch focus to the next screen, we then simply have to look up the first window, located on the next screen.