Search code examples
resizekey-bindingsgnome

gnome key binding one third window size


I'd like to create key-bindings that set the current window size to one third the screen size horizontally and maximal vertically and locate it on the left, middle, or right one third of the screen. How could I accomplish that?


Solution

  • xbindkeys and xdotool

    With xbindkeys you can use shortctus independently from the window manager. xdotool allows to move and resize windows.

    Install it by:

    sudo apt-get install xbindkeys 
    sudo apt-get install xdotool 
    

    Default Configuration

    The default setting are showing what the shortcut bindings might look like. Create it and take a look at the file contents.

    xbindkeys --defaults > ~/.xbindkeysrc
    

    Use a editor to edit ~/.xbindkeysrc and enter:

    "/bin/bash ~/placewindow.sh left"
        control + l
    
    "/bin/bash ~/placewindow.sh middle"
        control + m
    
    "/bin/bash ~/placewindow.sh right"
        control + r
    

    Create shell script

    Create a shell script with a text editor, for our use case I have named it ~/placewindow.sh:

    #!/bin/bash
    
    width=`xdpyinfo | grep dimensions | sed -r 's/^[^0-9]*(([0-9]+)x([0-9]+)).*$/\2/'`
    height=`xdpyinfo | grep dimensions | sed -r 's/^[^0-9]*(([0-9]+)x([0-9]+)).*$/\3/'`
    
    case "$1" in
        left)       
           xdotool windowsize `xdotool getactivewindow` `expr $width / 3` $height
           xdotool windowmove `xdotool getactivewindow` 0 0
           ;;
        middle)
           xdotool windowsize `xdotool getactivewindow` `expr $width / 3` $height
           xdotool windowmove `xdotool getactivewindow` `expr $width / 3` 0
           ;;
        right)
           xdotool windowsize `xdotool getactivewindow` `expr $width / 3` $height
           xdotool windowmove `xdotool getactivewindow` `expr $width \* 2 / 3` 0
           ;;
    esac
    

    Make it executable:

    chmod +x placewindow.sh
    

    Hints

    After making changes to ~/.xbindkeysrc you need to

    killall xbindkeys
    xbindkeys 
    

    to make the changes active immediately.

    Demo

    When pressing now CTRL+l, CTRL+m, or CTRL+r the active window gets sized and located. It looks like this:

    window sizing demo