Skip to content

Instantly share code, notes, and snippets.

@lucifr
Last active September 26, 2024 15:57
Show Gist options
  • Save lucifr/b0780e38045235027ef11746041dc120 to your computer and use it in GitHub Desktop.
Save lucifr/b0780e38045235027ef11746041dc120 to your computer and use it in GitHub Desktop.
My Hammerspoon config file
-- -------------------------------
-- Watcher for changes of init.lua
-- -------------------------------
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
local myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert("Hammerspoon config reloaded")
-- ------------------
-- Modifier shortcuts
-- ------------------
local alt = {"⌥"}
local hyper = {"⌘", "⌥", "⌃", "⇧"}
local nudgekey = {"⌥", "⌃"}
local yankkey = {"⌥", "⌃","⇧"}
local pushkey = {"⌃", "⌘", "⌥"}
local shiftpushkey= {"⌃", "⌘", "⇧"}
-- ---------------------
-- F19 and Hyperkey Hack
-- ---------------------
-- Keys you want to use with hyper key go here:
hyperBindings = {'t', 'f', 'e', 'v', 'c', 'p', '\\', 'j', 'k', 'h', 'l', 'q', 'TAB'}
k = hs.hotkey.modal.new({}, "F17")
for i,key in ipairs(hyperBindings) do
k:bind({}, key, nil, function() hs.eventtap.keyStroke(hyper, key)
k.triggered = true
end)
end
pressedF19 = function()
k.triggered = false
k:enter()
end
releasedF19 = function()
k:exit()
if not k.triggered then
hs.eventtap.keyStroke({}, 'F18')
end
end
f18 = hs.hotkey.bind({}, 'F19', pressedF19, releasedF19)
-- -----------------
-- Window management
-- -----------------
-- None of this animation shit:
hs.window.animationDuration = 0
-- Get list of screens and refresh that list whenever screens are plugged or unplugged:
local screens = hs.screen.allScreens()
local screenwatcher = hs.screen.watcher.new(function()
screens = hs.screen.allScreens()
end)
screenwatcher:start()
-- init grid
hs.grid.MARGINX = 0
hs.grid.MARGINY = 0
hs.grid.GRIDWIDTH = 7
hs.grid.GRIDHEIGHT = 3
-- Resize window for chunk of screen.
-- For x and y: use 0 to expand fully in that dimension, 0.5 to expand halfway
-- For w and h: use 1 for full, 0.5 for half
function push(x, y, w, h)
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + (max.w*x)
f.y = max.y + (max.h*y)
f.w = max.w*w
f.h = max.h*h
win:setFrame(f)
end
-- Show grid
hs.hotkey.bind(pushkey, 'g', hs.grid.show)
-- Push to screen edge
hs.hotkey.bind(pushkey,"left", function() push(0,0,0.5,1) end) -- left side
hs.hotkey.bind(pushkey,"right", function() push(0.5,0,0.5,1) end) -- right side
hs.hotkey.bind(pushkey,"pageup", function() push(0,0,1,0.5) end) -- top half
hs.hotkey.bind(pushkey,"pagedown", function() push(0,0.5,1,0.5) end) -- bottom half
-- Center window with some room to see the desktop
hs.hotkey.bind(pushkey, "m", function() push(0.05,0.05,0.9,0.9) end)
-- Fullscreen
hs.hotkey.bind(pushkey, "up", function() push(0,0,1,1) end)
-- -----------------
-- Applications
-- -----------------
-- Toggle named app's visibility, launching if needed
function toggle_app(name)
focused = hs.window.focusedWindow()
if focused then
app = focused:application()
if app:title() == name then
app:hide()
return
end
end
hs.application.launchOrFocus(name)
end
-- Applications, toggle visibility
hs.hotkey.bind(hyper, 'v', function() toggle_app('iTerm2') end)
hs.hotkey.bind(hyper, 'e', function() toggle_app('Sublime Text') end)
hs.hotkey.bind(hyper, 't', function() toggle_app('Tweetbot') end)
hs.hotkey.bind(hyper, 'f', function() toggle_app('Finder') end)
-- -----
-- Misc
-- -----
-- Vim-like keymaps
hs.hotkey.bind(hyper, 'j', function() hs.eventtap.keyStroke({}, 'down') end)
hs.hotkey.bind(hyper, 'k', function() hs.eventtap.keyStroke({}, 'up') end)
hs.hotkey.bind(hyper, 'h', function() hs.eventtap.keyStroke({}, 'left') end)
hs.hotkey.bind(hyper, 'l', function() hs.eventtap.keyStroke({}, 'right') end)
-- Help. Lists shortcuts, etc.
-- The terrible spacing looks fine when the alert is actually displayed
hs.hotkey.bind(hyper, "Q", function()
helpstr = [[Hyper ⌘⌥⌃⇧
Push ⌘⌃
Grid Hyper-G
Finder Hyper-F
Tweetbot Hyper-T
Sublime Text Hyper-E
iTerm2 Hyper-V]]
hs.alert.show(helpstr)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment