Snapping windows in Linux - osnap.py

Some time ago I bought a 24” display for my desktop. Because the 1920px width is way to large for any single window with the obvious exception of the movie player, without even realizing, I had changed the way I arrange windows: on a 19” display I keep a single large window visible at a time, maximized to cover the whole screen; on the 24” one I tend to keep two, even three windows visible, for example the browser on the left side of the screen, emacs on the upper right corner and a terminal on the lower right.

This, however, made me realize that resizing and moving windows by hand is a real nuisance. 

The snapping in Windows 7 is of great help with this, but has two drawbacks: 

  • it doesn’t allow you to snap to just a quarter of the screen
  • it requires Windows 7; I prefer to use Linux with Gnome

The only similar thing I could find for Linux was the Compiz Grid plugin, which looks great. Unfortunately, it also has two drawbacks:

  • it requires 8 keyboard shortcuts: 4 for snapping to the up/down/left/right half of the screen and another 4 for the corners, which means that you can’t simply use the arrow keys (I believe it’s designed to work with the numpad, which I kinda hate)
  • it requires Compiz, which I don’t use for some (very arguable) performance considerations

So I decided to make something that works just as I want it to. It actually ended up taking only a few hours and exactly 100 lines of Python, and here it is: osnap.py.

What is does in a nutshell is allow you to snap windows in a 4x4 grid using only keyboard shortcuts, to either a half or a corner of the screen, while also being able to restore windows to their old size and position from before the snap.  

And a little more detail:

After being ran, osnap stays resident and catches Win+Arrow key combinations (or Mod4+Arrow, for purists). When the key handler is triggered, osnap will resize the currently focused window to cover the half of the screen corresponding to the arrow key pressed. 

If the same shortcut is pressed again the window’s original size and position will be restored. 

If two key combinations are pressed in sequence quickly (under 250ms), the focused window will be resized to cover the corresponding *quarter* of the screen.

And that’s all there is to it. It works exactly how I want it to and it’s small. The code’s available on github.

Notes

Top