Skip to content

Instantly share code, notes, and snippets.

@hassy
Last active May 12, 2023 13:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hassy/74cb10bbd3ee175cfa2f8fcf315d1987 to your computer and use it in GitHub Desktop.
Save hassy/74cb10bbd3ee175cfa2f8fcf315d1987 to your computer and use it in GitHub Desktop.
Hammerspoon config

Hassy's Hammerspoon Config

Just like its website says Hammerspoon is staggeringly powerful (and also staggeringly fun). This is my Hammerspoon config. It's very simple, but it allowed me to replace two other apps: Spectacle for moving windows around, and Karabiner which I used for quickly switching between apps.

This is where it all began:

🎧 Have you ever had your bluetooth headphones disconnect and inadvertently & loudly share your unconventional music taste with the rest of the office/coffeeshop?

1. brew install hammerspoon
2. drop a few lines into ~/.hammerspoon/init.lua
3. never again! 🤫 pic.twitter.com/RsHJn17djR

— Hassy Veldstra (@hveldstra) March 11, 2020

Setting up

Install Hammerspoon:

brew cask install hammerspoon

Install MiroWindowManager spoon (add-on):

tmpfn="spoon.zip" ; tmpdn="`mktemp -d`" \
curl -L https://github.com/miromannino/miro-windows-manager/raw/master/MiroWindowsManager.spoon.zip --output "$tmpdn/$tmpfn" && \
unzip "$tmpdn/$tmpfn" -d "$tmpdn" && \
mv "$tmpdn/MiroWindowsManager.spoon" ~/.hammerspoon/Spoons

Drop the code from init.lua into ~/.hammerspoon/init.lua

--
-- Set volume to zero when headphones disconnect.
-- Prevents inadvertently sharing your tines with the rest of the office/coffeshop when
-- Bluetooth headphones disconnect.
--
hs.audiodevice.watcher.setCallback(function(evt)
if evt == "sOut" or evt == "dOut" then
hs.audiodevice.defaultOutputDevice():setVolume(0)
end
dev = hs.audiodevice.defaultOutputDevice()
name = dev:name()
if (string.find(name, "AirPods") > 0) then
hs.alert.show("AirPods connected")
end
end)
hs.audiodevice.watcher.start()
function testStuff()
end
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "/", testStuff)
--
-- Window management:
--
-- https://www.hammerspoon.org/Spoons/MiroWindowsManager.html
hs.loadSpoon("MiroWindowsManager")
local hyper = {"ctrl", "alt", "cmd"}
hs.window.animationDuration = 0.1
spoon.MiroWindowsManager:bindHotkeys({
up = {hyper, "up"},
right = {hyper, "right"},
down = {hyper, "down"},
left = {hyper, "left"},
fullscreen = {hyper, "f"}
})
-- Move to next physical screen:
hs.hotkey.bind(hyper, "k", function()
local win = hs.window.focusedWindow();
if not win then return end
win:moveToScreen(win:screen():next())
end)
--
-- Custom app switching:
--
local applicationHotkeys = {
c = 'Google Chrome',
t = 'iTerm',
n = 'Notes',
b = 'Bear',
p = 'KeePassXC',
x = 'Firefox',
e = 'Emacs'
}
-- Some apps like Emacs report themselves as a different name to macOS than
-- their application bundle, so we look the process up by name first, then focus
-- or launch:
for key, app in pairs(applicationHotkeys) do
hs.hotkey.bind(hyper, key, function()
local proc = hs.application.find(app)
if proc then
proc:mainWindow():focus()
else
hs.application.launchOrFocus(app)
end
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment