What is Hammerspoon? #
Hammerspoon is a macOS automation tool that lets you script window management, app launching, hotkeys, and more — using the Lua programming language.
Why tho? #
I am really enjoying living and breathing in CLI. Controlling my Mac with lua feels so much fun. I was able to replicate Rectangle window management as well automating some behaviour when an app is first launched.
Furthermore, I am able to set some shortcuts that lets me instantly launch an app and bring it into focus — even when my desktop is cluttered with a dozen open windows.
How it works? #
Download:
brew install --cask hammerspoon
A folder should be created at ~/.hammerspoon.
Create init.lua
local hotkey = require("hs.hotkey")
-- hyper key
local hyper = { "cmd", "ctrl" }
Basically the above is your hyper key. To activate anything from hammer spoon, you can press cmd + ctrl + (key you set for the function)
I created a reload bind to quickly reload my hammerspoon init.lua
hotkey.bind(hyper, "`", function()
hs.reload()
end)
hs.alert.show("Config loaded")
To replicate Rectangle’s window behaviour you can do the following:
-- middle left
hs.hotkey.bind(hyper, "Left", function()
hs.window.focusedWindow():moveToUnit({ 0, 0, 0.5, 1 })
end)
-- middle right
hs.hotkey.bind(hyper, "Right", function()
hs.window.focusedWindow():moveToUnit({ 0.5, 0, 0.5, 1 })
end)
-- middle up
hs.hotkey.bind(hyper, "Up", function()
hs.window.focusedWindow():moveToUnit({ 0, 0, 1, 0.5 })
end)
-- middle down
hs.hotkey.bind(hyper, "Down", function()
hs.window.focusedWindow():moveToUnit({ 0, 0.5, 1, 0.5 })
end)
Now press hyper key (cmd + ctrl) + left, right, up, down to snap the windows around your desktop.
You can do some crazy stuff with hammerspoon, I’m looking to automate some app workflow next. With Mac, it’s kinda annoying to move it to another workspace when you launch it first time. Hammerspoon will allow me to set app specific space.