Hammerspoon
Back to System
HammerspoonIntermediate12 min

Hammerspoon Window Management Guide: macOS Layouts and Snapping with Lua

Why Hammerspoon for Windows?

Hammerspoon is a macOS automation tool that uses Lua scripts to control windows, apps, and system events. For window management, it's more powerful than any dedicated app because you write exactly the rules you need.

This guide builds a complete window management system: edge snapping, grid layouts, multi-monitor support, and custom shortcuts — all in pure Lua.

Installation & init.lua

Install Hammerspoon from hammerspoon.org or via Homebrew:

brew install --cask hammerspoon

Launch Hammerspoon, grant accessibility permissions in System Settings → Privacy & Security, then open the config file:

~/.hammerspoon/init.lua

Window Layouts with hs.window

The hs.window module is your primary tool. Start with edge snapping — the most useful window management feature.

local wf = hs.window.filter
local half = hs.layout.left50
-- Snap to left half
hs.hotkey.bind({"cmd","alt"}, "left", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen():frame()
f.x = screen.x
f.y = screen.y
f.w = screen.w / 2
f.h = screen.h
win:setFrame(f)
end)
-- Snap to right half
hs.hotkey.bind({"cmd","alt"}, "right", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen():frame()
f.x = screen.x + screen.w / 2
f.y = screen.y
f.w = screen.w / 2
f.h = screen.h
win:setFrame(f)
end)

Automated Snapping & Grids

For multi-monitor setups, moving windows between displays is essential. Add full-screen toggle and monitor switching:

Pro tip: Use hs.window.animationDuration = 0 to disable animation lag. Windows snap instantly.

Save and restore layouts with hs.layout:

local layout = {
{"Safari", nil, display1, hs.layout.left50, nil, nil},
{"Terminal", nil, display1, hs.layout.right50, nil, nil},
{"Slack", nil, display2, hs.layout.maximized, nil, nil},
}
hs.hotkey.bind({"cmd","alt"}, "0", function()
hs.layout.apply(layout)
end)

Multi-Monitor Layout Logic

Go beyond basic snapping with these advanced techniques:

Layout Bugs & Edge Cases

Window management issues and solutions:

Your Window Workflow

Your window management system is ready. Next steps:

← PreviousNeovim Lua Configuration Guide 2026: Setup from Scratch with lazy.nvimNext →Karabiner-Elements Tutorial: Remap macOS Keyboard and Build a Hyper Key