How to use Yabai with Hammerspoon - Part 1

2021/04/02

Yabai is a tiling window manager for MacOS. Tiling window managers automatically tile your screen to fill the space with all active windows.

I found out about Yabai after using Amethyst for the same purpose. While Amethyst is more user friendly, it lacks a text based configuration which Yabai offers.

When I started using Yabai there was no documented solution on how to use Yabai window movement with Hammerspoon.

The main when calling Yabai from Hammerspoon is how long Hammerspoon will need to issue commands to Yabai. That time ranges between 1-2s if you just use hs.execute. Use this function access Yabai from Hammerspoon way faster:

function yabai(args)

  hs.task.new("/usr/local/bin/yabai",nil, function(ud, ...)
    print("stream", hs.inspect(table.pack(...)))
    return true
  end, args):start()

end

My Hyper-Key is bound to Capslock using Karabiner Elements. And with these bindings I can quickly switch to the windows north, south, west and east of my current window:

hs.hotkey.bind(HYPER, "j", function() yabai({"-m", "window", "--focus", "north"}) end)
hs.hotkey.bind(HYPER, "k", function() yabai({"-m", "window", "--focus", "south"}) end)
hs.hotkey.bind(HYPER, "h", function() yabai({"-m", "window", "--focus", "west"}) end)
hs.hotkey.bind(HYPER, "l", function() yabai({"-m", "window", "--focus", "east"}) end)

See in a future post how I control tmux splits with the same keybindings.