Skip to content

Your first config ​

Hazel does nothing until your config tells it to. No key opens a terminal, nothing gets focus, new windows sit at the top-left corner. This page writes a config that does those things. Every recipe after it adds to this one.

The config is Lua 5.4, in ~/.config/hazel/main.lua.

Try it nested ​

Run hazel from a terminal in your current session. It opens as a window there, with its own outputs, and your desktop keeps running around it. Close the window, or press the quit key below, to stop it.

Your own compositor sees keys before the Hazel window does, so its Super bindings win. While you try things nested, set mod below to "alt".

Hazel reloads the config whenever you save it, so leave it running while you edit.

The config ​

lua
local mod = "super"
local terminal = "foot"

local spawn_env = {}

local function spawn(program, args)
	local started, result = pcall(Process.spawn, program, args, { env = spawn_env })
	if not started then print(tostring(result):match("[^\n]*")) end
	return started and result or nil
end

local function seat()
	return wm.input.seats:get(1)
end

local function focused_window()
	local target = seat() and seat():focused()
	if target and target.kind == "window" then return target end
end

local function focus(window)
	if not seat() then return end
	seat():focus(window)
	if window then window.box:raise() end
end

local bindings = {}

local function bind(modifiers, key, action)
	local wanted = {}
	for name in modifiers:gmatch("%a+") do wanted[name] = true end
	bindings[#bindings + 1] = { modifiers = wanted, key = Key[key], action = action }
end

local function matches(binding, event)
	if event.raw_key ~= binding.key then return false end
	for _, name in ipairs({ "super", "shift", "ctrl", "alt" }) do
		if (binding.modifiers[name] or false) ~= event.modifiers[name] then return false end
	end
	return true
end

wm.input:on("key", function(event)
	if wm.is_session_locked or event.shortcuts_inhibited then return end
	for _, binding in ipairs(bindings) do
		if matches(binding, event) then
			event:prevent_default()
			if event.state == "Pressed" then binding.action() end
			return
		end
	end
end)

local function output_under_pointer()
	local pointer = seat() and seat():pointer()
	return pointer and wm.outputs:under(pointer:position())[1] or wm.outputs:get(1)
end

local function output_of(window)
	local size = window.size or { width = 0, height = 0 }
	local center = window.box.position:target() + Point(size.width / 2, size.height / 2)
	return wm.outputs:under(center)[1] or wm.outputs:get(1)
end

local function focus_topmost()
	local windows = wm.windows:all()
	for index = #windows, 1, -1 do
		local window = windows[index]
		if window.box.visible and window.has_content then return focus(window) end
	end
end

wm.windows:on("new_window", function(window)
	window:on("first_content", function(window)
		local area = output_under_pointer():usable_area()
		local size = window.size
		window.box.position = Point(
			math.floor(area.x + (area.width - size.width) / 2),
			math.floor(area.y + (area.height - size.height) / 2)
		)
		focus(window)
	end)
	window:on("closed", function()
		if not focused_window() then focus_topmost() end
	end)
end)

wm.input:on("pointer_button_forwarded", function(event)
	if event.state ~= "Pressed" then return end
	local clicked = event.pointer:window_under()
	if clicked then focus(clicked) end
end)

bind(mod, "Return", function() spawn(terminal) end)
bind(mod, "q", function()
	local window = focused_window()
	if window then window:close() end
end)
bind(mod .. " shift", "r", function() wm:reload() end)
bind(mod .. " shift", "e", function() wm:quit() end)
KeysDoes
mod + Returnopens terminal
mod + Qcloses the focused window
mod + Shift + Rreloads the config
mod + Shift + Equits Hazel

What each part does ​

  • spawn starts a program with spawn_env added to its environment. Session puts DISPLAY there for X apps. A program that isn't installed is logged instead of stopping the config.
  • bind adds a key binding. matches checks the key and that exactly those modifiers are held. The key handler runs the first binding that matches, and keeps both the press and the release from the app.
  • Bindings don't run on the lock screen or while an app such as a VM viewer holds the keyboard (event.shortcuts_inhibited).
  • A new window has no size until it first draws. first_content is the first moment it has one, so it is centred and focused there. After a reload first_content fires again for every open window, so floating windows are centred again each time you save.
  • When the focused window closes, focus goes to the topmost window left.
  • output_of finds the output a window's centre is on. Recipes use it.
  • A click focuses and raises the window under the pointer. The click still reaches the app.
  • Key[...] takes xkb key names: "Return", "q", "F1", "XF86AudioMute".

The recipes use mod, spawn, spawn_env, seat, focused_window, focus, focus_topmost, output_of, output_under_pointer and bind from here. Paste them under this config.

If it doesn't load ​

A config with an error doesn't replace the one that's running. The error goes to the log with a file and line, and Hazel keeps going. Fix the file and save it again. Debugging has more.

Hazel stops at start if main.lua doesn't exist:

text
Hazel has no config: /home/you/.config/hazel/main.lua does not exist; create it, or set HAZEL_COMPOSITOR_DIR

To try a config somewhere else, point HAZEL_COMPOSITOR_DIR at its directory:

sh
HAZEL_COMPOSITOR_DIR=~/hazel-test hazel