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?

6
  • Sure, can be done via a script or two . If I understood correctly , you just want a shortcut that will make window jump from one monitor to the other in your dual-screen setup. Is that correct ? – Sergiy Kolodyazhnyy Jul 28 '16 at 21:52
  • Serg, yes that's correct. – jtlindsey Jul 28 '16 at 21:56
  • OK , I will try to sketch up a script. Probably will post it tomorrow due to a few time issues. – Sergiy Kolodyazhnyy Jul 28 '16 at 21:59
  • Posted an answer with instructions. Please let me know if this works alright for you. I've been testing it on my dual-screen setup. The code should work for more than two screens , but i don't have access to extra hardware unfortunately. Let me know if you have any other questions – Sergiy Kolodyazhnyy Jul 30 '16 at 6:49
  • 3
    Possible duplicate of Move windows between screens in dual screen mode – Jacob Vlijm Apr 6 '17 at 12:06
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)
7
  • 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 8: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 8: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
  • 1
    @Serg i just got home and tested it on Ubuntu Mate. It works great with my dual monitor setup. Great scripting :-) – jtlindsey Aug 2 '16 at 22:32
  • 1
    In case you receive "ImportError: No module named gi" run "sudo apt install python-gi". – Dariusz Apr 21 '18 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.