Skip to content

Instantly share code, notes, and snippets.

@raveren
Created June 8, 2021 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raveren/e5590b95c853c92225eab5312df5b5e8 to your computer and use it in GitHub Desktop.
Save raveren/e5590b95c853c92225eab5312df5b5e8 to your computer and use it in GitHub Desktop.
Hammerspoon personal config
-- Define some keyboard modifier variables
-- (Node: Capslock bound to cmd+alt+ctrl+shift via Seil and Karabiner)
local hyper = {"⌘", "⌥", "⌃", "⇧"}
-- Reload config
function reloadConfig(paths)
doReload = false
for _,file in pairs(paths) do
if file:sub(-4) == ".lua" then
print("A lua file changed, doing reload")
doReload = true
end
end
if not doReload then
print("No lua file changed, skipping reload")
return
end
hs.reload()
end
-- ping
hs.hotkey.bind(hyper, "p", function()
hs.network.ping.ping("8.8.8.8", 1, 0.01, 1.0, "any", function (object, message, seqnum, error)
if message == "didFinish" then
avg = tonumber(string.match(object:summary(), '/(%d+.%d+)/'))
if avg == 0.0 then
hs.alert.show("No network")
elseif avg < 200.0 then
hs.alert.show("Network good (" .. avg .. "ms)")
elseif avg < 500.0 then
hs.alert.show("Network poor(" .. avg .. "ms)")
else
hs.alert.show("Network bad(" .. avg .. "ms)")
end
end
end)
end)
-- this one for lock the screen
hs.hotkey.bind(hyper, "l", function()
hs.caffeinate.lockScreen()
end)
-- hs.loadSpoon("Caffeine")
-- spoon.Caffeine:bindHotkeys({toggle = {HYPER, "I"},})
-- Hotkeys to move windows between screens, retaining their position/size relative to the screen
--hs.urlevent.bind('hyperfnleft', funcStion() hs.window.focusedWindow():moveOneScreenWest() end)
-- hs.urlevent.bind('hyperfnright', function() hs.window.focusedWindow():moveOneScreenEast() end)
-- Hotkeys to resize windows absolutely
-- hs.hotkey.bind(hyper, "'", function() hs.window.focusedWindow():moveToUnit(hs.layout.left30) end)
-- hs.hotkey.bind(hyper, ';', function() hs.window.focusedWindow():moveToUnit(hs.layout.right70) end)
-- hs.hotkey.bind(hyper, '[', function() hs.window.focusedWindow():moveToUnit(hs.layout.left50) end)
-- hs.hotkey.bind(hyper, ']', function() hs.window.focusedWindow():moveToUnit(hs.layout.right50) end)
-- Toggle a window between its normal size, and being maximized
local frameCache = {}
hs.hotkey.bind(hyper, 'j', function ()
local win = hs.window.focusedWindow()
if frameCache[win:id()] then
win:setFrame(frameCache[win:id()])
frameCache[win:id()] = nil
else
frameCache[win:id()] = win:frame()
hs.alert.show(frameCache[win:id()])
win:maximize()
end
end)
-- Defines for window grid
hs.grid.GRIDWIDTH = 3
hs.grid.GRIDHEIGHT = 3
hs.grid.MARGINX = 0
hs.grid.MARGINY = 0
hs.hotkey.bind(hyper, '=', hs.grid.show)
hs.hotkey.bind(hyper, 'Left', hs.grid.pushWindowLeft)
hs.hotkey.bind(hyper, 'Right', hs.grid.pushWindowRight)
hs.hotkey.bind(hyper, 'Up', hs.grid.pushWindowUp)
hs.hotkey.bind(hyper, 'Down', hs.grid.pushWindowDown)
local hotkeys = {
s = "com.sublimetext.4",
w = "com.jetbrains.PhpStorm",
q = "org.mozilla.firefox",
a = "org.mozilla.firefox",
c = "com.google.Chrome",
e = "com.apple.finder",
}
for key, app in pairs(hotkeys) do
hs.hotkey.bind(hyper, key, function()
hs.application.launchOrFocusByBundleID(app)
end)
end
function applicationWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
if (appName == "Finder" or appName == "Google Chrome") then
-- Bring all Finder windows forward when one gets activated
appObject:selectMenuItem({"Window", "Bring All to Front"})
end
if (appName == "PhpStorm") then
hs.keycodes.setLayout("Unicode Hex Input")
end
end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()
-- Misc hotkeys
-- hs.hotkey.bind(hyper, 'n', function() os.execute("open ~") end)
-- Type the current clipboard, to get around web forms that don't let you paste
-- (Note: I have Fn-v mapped to F17 in Karabiner)
hs.urlevent.bind('fnv', function() hs.eventtap.keyStrokes(hs.pasteboard.getContents()) end)
configFileWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig)
configFileWatcher:start()
-- Finally, show a notification that we finished loading the config successfully
hs.notify.new({
title='Hammerspoon',
informativeText='Config loaded'
}):send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment