Create keyboard shortcuts to move windows to different monitors without Compiz

Solution 1:

Introduction

window_jumper.py is a python script that will move active window across multiple monitors in cycle. For instance, if you have 3 monitors A,B, and C , repeated keypress of the assigned shortcut will move the window from A, to B, to C, and back to A. The window placement will be Top Left corner of each screen.

Usage

To run script manually

python window_jumper.py

The script has no command line options ( as of right now , but may in future ).

Setting Up Keyboard Shortcut

Ubuntu Unity steps:

  1. Go to System Settings -> KeyboardShortcuts tab , select Custom Shortcuts and click + button. Custom Shortcut popup will appear with two fields Name: and Command:

  2. For Name field , call it window_jumper. For Command: provide full path to the script file. For instance, python /home/ubuntu_user/bin/window_jumper.py . Click Apply

  3. Click on the right-most column , the words New accelerator will appear. Press the keyboard shortcut that you wish to be designated to this script. For instance , I chose CtrlSuperJ

Ubuntu Mate instructions:

  1. Go to SystemControl CenterKeyboard Shortcuts , click Add. Custom Shortcut popup will appear with two fields Name: and Command:

  2. For Name field , call it window_jumper. For Command: provide full path to the script file. For instance, python /home/ubuntu_user/bin/window_jumper.py . Click Apply

  3. Right-most column (labeled Shortcut) will have words Disabled on the line. Click on the words, the text will change to New shortcut. Press the key combination you wish to use.

Script source

Also available as on GitHub. If you have GitHub account, please submit issues and feature requests there.

#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GdkX11, Gdk, Gtk


def main():

    DEBUG = False

    screen = GdkX11.X11Screen.get_default()
    monitors = []
    for monitor in range(screen.get_n_monitors()):
        monitors.append(
            [screen.get_monitor_geometry(monitor).x,
             screen.get_monitor_geometry(monitor).y])

    if DEBUG:
        print monitors

    active_window = screen.get_active_window()
    active_window_location = screen.get_monitor_at_window(active_window)

    new_location = None
    new_location = active_window_location + 1
    if active_window_location + 1 >= monitors.__len__():
        new_location = 0
    new_screen = monitors[new_location]
    if DEBUG:
        print new_screen

    active_window.move(new_screen[0], new_screen[1])
    screen.get_active_window()
    # TODO: add resizing window routine in cases where
    # a window is larger than the size of the screen
    # to which we're moving it.

if __name__ == "__main__":
    main()

Side notes:

  • The code may or may not change to include additional features.
  • In case you receive ImportError: No module named gi run sudo apt install python-gi (Thanks Dariusz for the comment)