Skip to content

Instantly share code, notes, and snippets.

@larrybolt
Created October 14, 2019 19:36
Show Gist options
  • Save larrybolt/2eeec7f7e2030a18bd90e20419ff50ec to your computer and use it in GitHub Desktop.
Save larrybolt/2eeec7f7e2030a18bd90e20419ff50ec to your computer and use it in GitHub Desktop.
hammerspoon
-----------------------------------------------
-- Set up
-----------------------------------------------
local hyper = {"shift", "cmd", "alt", "ctrl"}
-- dofile("anycomplete/anycomplete.lua")
dofile("/Users/larrybolt/.dotfiles/hammerspoon/anycomplete/anycomplete.lua")
-----------------------------------------------
-- Keyboard layout
-- physical be, software us qwerty:
-- § 1 2 3 4 5 6 7 8 9 0 - = [ bsp ]
-- tab q w e r t y u i o p [ ] | ret|
-- caps a s d f g h j k l ; ' \ |___|
-- sh ` z x c v b n m , . / [ shift]
-- fn ct alt cmd [space] cmd alt arrow
-----------------------------------------------
-- Hyper = shift + cmd + alt + ctrl = tab
-- (tab works as tab when not combined,
-- as hyper when modifier)
--
-- Hyper+q left
-- Hyper+w full
-- Hyper+e right
-- Hyper+r show hints
--
-- Hyper+[] move current app to previous/next screen
--
-- Hyper+; show Hammerspoon console
--
--
-- Hyper+hjkl for vim-alike focus switching
--
-----------------------------------------------
-- App shortcuts
local key2App = {
b = 'Emacs',
-- v = 'MacVim',
v = 'Visual Studio Code',
n = 'Google Chrome',
i = 'IntelliJ IDEA',
f = 'Franz',
t = 'iTerm'
}
for key, app in pairs(key2App) do
hs.hotkey.bind(hyper, key, function() hs.application.launchOrFocus(app) end)
end
-- Settings
hs.window.animationDuration = 0
-- Hyper+qwe for left,full,right
hs.hotkey.bind(hyper, "q", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrame(f)
end)
hs.hotkey.bind(hyper, "e", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + (max.w / 2)
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrame(f)
end)
hs.hotkey.bind(hyper, "w", function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w
f.h = max.h
win:setFrame(f)
end)
-- Hyper+[] for screen switching
hs.hotkey.bind(hyper, "[", function()
local win = hs.window.focusedWindow()
local newscreen = win:screen():next()
win:moveToScreen(newscreen)
end)
hs.hotkey.bind(hyper, "]", function()
local win = hs.window.focusedWindow()
local newscreen = win:screen():previous()
win:moveToScreen(newscreen)
end)
-- Hyper r for hints
hs.hotkey.bind(hyper, "r", function()
hs.hints.windowHints()
end)
-- Reload config on write
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")
-- console
hs.hotkey.bind(hyper, ';', hs.openConsole)
-- Bring all Finder windows forward when one gets activated
function applicationWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
if (appName == "Finder") then
-- Bring all Finder windows forward when one gets activated
appObject:selectMenuItem({"Window", "Bring All to Front"})
end
end
end
local appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()
-- Hyper hjkl to switch window focus
hs.hotkey.bind(hyper, 'h', function()
hs.window.focusedWindow():focusWindowWest()
end)
hs.hotkey.bind(hyper, 'j', function()
hs.window.focusedWindow():focusWindowSouth()
end)
hs.hotkey.bind(hyper, 'k', function()
hs.window.focusedWindow():focusWindowNorth()
end)
hs.hotkey.bind(hyper, 'l', function()
hs.window.focusedWindow():focusWindowEast()
end)
-- Bluetooth on when I connect my keyboard
local usbWatcher = nil
function usbDeviceCallback(data)
if (data["productName"] == "QuickFire Rapid keyboard") then
if (data["eventType"] == "added") then
os.execute("/usr/local/bin/blueutil power 1")
elseif (data["eventType"] == "removed") then
os.execute("/usr/local/bin/blueutil power 0")
end
else
hs.alert.show(data["eventType"] .. " usb " .. data["productName"])
end
end
usbWatcher = hs.usb.watcher.new(usbDeviceCallback)
usbWatcher:start()
-- WiFi
local home = {["Mikrotik"] = TRUE, ["Mikrotik_outside"] = TRUE}
local lastSSID = hs.wifi.currentNetwork()
function ssidChangedCallback()
newSSID = hs.wifi.currentNetwork()
if home[newSSID] and not home[lastSSID] then
-- We just joined our home WiFi network
hs.alert.show("Welcome home")
hs.audiodevice.defaultOutputDevice():setVolume(25)
elseif not home[newSSID] and home[lastSSID] then
-- We just departed our home WiFi network
hs.audiodevice.defaultOutputDevice():setVolume(0)
end
lastSSID = newSSID
end
local wifiWatcher = hs.wifi.watcher.new(ssidChangedCallback)
-- wifiWatcher:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment