Ubuntu – Create keyboard shortcuts to move windows to different monitors without Compiz

mateshortcut-keysubuntu-matewindow-manager

I'm running Ubuntu MATE 16.04 and I love it right out of the box. The only thing I need on it is the ability to move my windows to different monitors using the keyboard.

I've been using CompizConfig Settings Manager and it works, but Compiz causes a long list of problems on my system that disappear when I disable it. Compiz is a big package and all I want is the ability to move my windows to a different monitor. All other features are already built into Ubuntu MATE 16.04 LTS keyboard shortcuts menu (switch workspaces, move window to workspace, tile left, right, horizontal, vertical, etc).

Move window east (right) side of screen and Move window west (left) side of screen for some reason don't work on all applications (notably Firefox, but Chrome and others work).

Is there a guide that shows how to make your own keyboard shortcuts? I found a site where someone made their own for Xubuntu. While I'm trying to figure out what they did, does anyone have a simple method for creating keyboard shortcuts that move windows between monitors?

Best Answer

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)