Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Created October 4, 2015 19:53
Show Gist options
  • Save iolloyd/2fb96386dd5bf1f339a8 to your computer and use it in GitHub Desktop.
Save iolloyd/2fb96386dd5bf1f339a8 to your computer and use it in GitHub Desktop.
Explanation of using hammerspoon to watch for configuration changes
Simple configuration reloading
You may have noticed that while you're editing the config, it's a little bit annoying to have to keep choosing the Reload Config menu item every time you make a change. We can fix that by adding a keyboard shortcut to reload the config:
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "R", function()
hs.reload()
end)
hs.alert.show("Config loaded")
We have now bound ⌘+⌥+⌃+R to a function that will reload the config and display a simple alert banner on the screen for a couple of seconds.
One important detail to call out here is that hs.reload() destroys the current Lua interpreter and creates a new one. If we had any code after hs.reload() in this function, it would not be called.
Fancy configuration reloading
So we can now manually force a reload, but why should we even have to do that when the computer could do it for us‽
The following snippet introduces another new extension, pathwatcher which will allow us to automatically reload the config whenever the file changes:
function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
hs.reload()
end
end
hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment