7

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?

jtlindsey
  • 1,952
  • 2
  • 19
  • 29

1 Answers1

8

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)
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Works nicely! I didn't know 1 GdkX11. Is new_location = None necessary? It is defined in the next line anyway. – Jacob Vlijm Jul 30 '16 at 08:21
  • @JacobVlijm it's my force of habit to declare variables before using them, since i come from C background. But it's not necessary here. It is however necessary with loops, I've ran into this issue in past. GdkX11 is nice although there are some quirks. At least it doesn't require extra package installation. – Sergiy Kolodyazhnyy Jul 30 '16 at 08:34
  • Not sure about that, I don't believe python-gi is installed by default. Wait... it must be. – Jacob Vlijm Jul 30 '16 at 12:52
  • Thanks a lot @Serg, very nicely done. I have am and have been out of town since you posted this. I can't wait till I get back to my setup to use it. – jtlindsey Aug 02 '16 at 16:14
  • @jtlindsey test it out, let me know how you like it, if you have any other suggestions or questions let me know . If it solves your question, please mark it as accepted answer. – Sergiy Kolodyazhnyy Aug 02 '16 at 20:20
  • 1
    @Serg i just got home and tested it on Ubuntu Mate. It works great with my dual monitor setup. Great scripting :-) – jtlindsey Aug 02 '16 at 22:32
  • 1
    In case you receive "ImportError: No module named gi" run "sudo apt install python-gi". – Velocoder Apr 21 '18 at 17:40