Skip to content

Instantly share code, notes, and snippets.

@cliffrowley
Last active July 8, 2020 02:13
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 cliffrowley/dba0ef9052e618f58c91523eaaa7c08d to your computer and use it in GitHub Desktop.
Save cliffrowley/dba0ef9052e618f58c91523eaaa7c08d to your computer and use it in GitHub Desktop.
My Hammerspoon config
--
-- Creates an application hotkey.
--
function bind_hotkey(mods, key, name)
return hs.hotkey.bind(mods, key, name, function ()
hs.application.open(name)
end)
end
--
-- Create application hotkeys.
--
bind_hotkey({"alt", "cmd"}, "§", "iTerm")
bind_hotkey({"alt", "cmd"}, "F1", "Messages")
bind_hotkey({"alt", "cmd"}, "F2", "Textual 7")
bind_hotkey({"alt", "cmd"}, "F3", "Slack")
bind_hotkey({"alt", "cmd"}, "F4", "Discord")
bind_hotkey({"alt", "cmd"}, "F5", "Scrivener")
-- bind_hotkey({"ctrl", "alt", "cmd"}, "/", "Dash")
--
-- Set grid options.
--
grid_margins = hs.geometry.size(5, 5)
grid_size = hs.geometry.size(10, 2)
hs.grid.setGrid(tostring(grid_size.w) .. "x" .. tostring(grid_size.h))
hs.grid.setMargins(grid_margins)
--
-- Moves the focused window to the given grid position.
--
function move_grid_window(dir, col, icon)
local win = hs.window.focusedWindow()
if win then
if col == 0 then
col = 10
end
if dir == "left" then
hs.grid.set(win, {x=0, y=0, w=col, h=grid_size.h})
hs.grid.snap(win)
elseif dir == "right" then
hs.grid.set(win, {x=grid_size.w-col, y=0, w=col, h=grid_size.h})
hs.grid.snap(win)
end
end
end
--
-- Creates a grid hotkey.
--
function create_grid_hotkey(key, icon)
local outer_hk = hs.hotkey.modal.new({"ctrl", "alt", "cmd"}, key)
function outer_hk:entered()
hs.alert.show("Grid " .. icon)
end
outer_hk:bind("", "escape", function()
hs.alert.show("Cancelled")
outer_hk:exit()
end)
for i = 0, grid_size.w - 1 do
outer_hk:bind("", tostring(i), function()
hs.alert.show("Grid " .. tostring(i) .. " " .. icon)
move_grid_window(key, i, icon)
outer_hk:exit()
end)
end
end
--
-- Create left grid hotkey.
--
create_grid_hotkey("left", "\xE2\xAC\x85")
--
-- Create right grid hotkey.
--
create_grid_hotkey("right", "\xE2\x9E\xA1")
--
-- Set the focused window to upper 50%.
--
up_hk = hs.hotkey.bind({"ctrl", "alt", "cmd"}, "up", function()
win = hs.window.focusedWindow()
if win then
g = hs.grid.get(win)
g.y = 0
g.h = 1
hs.grid.set(win, g)
end
end)
--
-- Set the focused window to lower 50%.
--
down_hk = hs.hotkey.bind({"ctrl", "alt", "cmd"}, "down", function()
win = hs.window.focusedWindow()
if win then
g = hs.grid.get(win)
g.y = 1
g.h = 1
hs.grid.set(win, g)
end
end)
--
-- Move the focused window left 1 screen.
--
hs.hotkey.bind({"shift", "ctrl", "alt", "cmd"}, "left", function()
win = hs.window.focusedWindow()
if win then
win:moveOneScreenWest()
end
end)
--
-- Move the focused window right 1 screen.
--
hs.hotkey.bind({"shift", "ctrl", "alt", "cmd"}, "right", function()
win = hs.window.focusedWindow()
if win then
win:moveOneScreenEast()
end
end)
--
-- Move the focused window up 1 screen.
--
hs.hotkey.bind({"shift", "ctrl", "alt", "cmd"}, "up", function()
win = hs.window.focusedWindow()
if win then
win:moveOneScreenNorth()
end
end)
--
-- Move the focused window down 1 screen.
--
hs.hotkey.bind({"shift", "ctrl", "alt", "cmd"}, "down", function()
win = hs.window.focusedWindow()
if win then
win:moveOneScreenSouth()
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment