DEV Community

loading...

Yabai - Toggle Window

slapdash
・2 min read

Window Management is no doubt the most boring stuff that no one wants to be doing. I used to have various shortcuts and hot corners set, to size up/down the window as needed. Magnet, Moom, Spectacle were amongst the few that i used. Then i discovered Yabai, it blew my mind off. You no longer need to remember all the crazy shortcuts. The windows are neatly tiled for you to work on and as a bonus those handful of not needed shortcuts could now be used for other important actions.

Yabai automatically partitions your windows using Binary Space Partitioning. There are lot of tutorials dedicated to configure and setup Yabai. I would suggest you go look into it first. Along with Yabai please do look into SKHD, this helps you setup the hotkeys to manipulate the Yabai calls.

Now, lets say you are comfortable with Yababi and it's inner workings including SKHD, I would like to share a small snippet that i use all the time to quickly float and toggle the size of the window with just a single shortcut.

resize.sh
#!/bin/sh

    xpt=$(yabai -m query --windows --window | jq -re '. | .frame.x')

    _log() {
        terminal-notifier -message $1
    }

    _big() {
        yabai -m window --grid 16:32:4:2:24:12
        _log 'big'
    }

    _medium() {
        yabai -m window --grid 16:32:6:2:20:12
        _log 'medium'
    }

    _small() {
        yabai -m window --grid 16:32:8:3:16:10
        _log 'small'
    }

    [[ $xpt -gt 481 ]] && _small
    [[ $xpt -le 481 && $xpt -gt 361 ]] && _medium
    [[ $xpt -le 361 && $xpt -gt 241 ]] && _big
    [[ $xpt -le 241 ]] && _small
Enter fullscreen mode Exit fullscreen mode

Query the current active window to retrieve the window coordinates. It uses jq to parse the JSON output that the yabai command spits out. xpt=$(yabai -m query --windows --window | jq -re '. | .frame.x')

Yabai uses the grid system to understand the window size. I have defined a small window as follows yabai -m window --grid 16:32:8:3:16:10.

Define various reference points as the x coordinates (xpt) to anchor the size of the window. Eg. in my setup the small window size falls exactly at the 481 pixel points. Use the range to define the toggle that cycles between big, medium and small.

    [[ $xpt -gt 481 ]] && _small
    [[ $xpt -le 481 && $xpt -gt 361 ]] && _medium
    [[ $xpt -le 361 && $xpt -gt 241 ]] && _big
    [[ $xpt -le 241 ]] && _small
Enter fullscreen mode Exit fullscreen mode

Now, the final part to trigger this shell script, i have assigned a hot key combination with skhd as follows ctrl + cmd - c : ~/bin/resize.sh. You need to add this to your skhdrc (configuration file for SKHD).

This is a small inspiration and there are much more to play around with this amazing window tiling manager.

References

Discussion (0)