Skip to content

Instantly share code, notes, and snippets.

@andyvanee
Last active April 4, 2024 18:51
Show Gist options
  • Save andyvanee/3ed44722d3ba1f575fd3376558b173a2 to your computer and use it in GitHub Desktop.
Save andyvanee/3ed44722d3ba1f575fd3376558b173a2 to your computer and use it in GitHub Desktop.
Hammerspoon config for window management
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "R", function() hs.reload() end)
hs.alert.show("Config loaded")
-- Fullscreen
hs.hotkey.bind({"cmd", "alt"}, "1",
function()
local window, screen = getFocusedWindowAndScreen()
window.w = screen.w
window.x = screen.x
window.y = screen.y
window.h = screen.h
hs.window.focusedWindow():setFrame(window, 0)
end
)
-- Half width left
hs.hotkey.bind({"cmd", "alt"}, "2",
function()
-- Left
local window, screen = getFocusedWindowAndScreen()
window.w = math.floor(screen.w / 2)
window.h = screen.h
window.x = screen.x
window.y = 0
hs.window.focusedWindow():setFrame(window, 0)
end
)
-- Half width right
hs.hotkey.bind({"cmd", "alt"}, "3",
function()
-- Right
local window, screen = getFocusedWindowAndScreen()
window.w = math.floor(screen.w / 2)
window.h = screen.h
window.x = screen.x + math.floor(screen.w / 2)
window.y = 0
hs.window.focusedWindow():setFrame(window, 0)
end
)
-- 4x4 grid: Top left
hs.hotkey.bind({"cmd", "alt"}, "4",
function()
local window, screen = getFocusedWindowAndScreen()
local halfWidth = math.floor(screen.w / 2)
window.w = halfWidth
window.h = math.floor(screen.h / 2)
window.x = screen.x
window.y = screen.y
hs.window.focusedWindow():setFrame(window, 0)
end
)
-- 4x4 grid: Top right
hs.hotkey.bind({"cmd", "alt"}, "5",
function()
local window, screen = getFocusedWindowAndScreen()
local halfWidth = math.floor(screen.w / 2)
window.w = halfWidth
window.h = math.floor(screen.h / 2)
window.x = screen.x + halfWidth
window.y = screen.y
hs.window.focusedWindow():setFrame(window, 0)
end
)
-- 4x4 grid: Bottom left
hs.hotkey.bind({"cmd", "alt"}, "6",
function()
local window, screen = getFocusedWindowAndScreen()
local halfWidth = math.floor(screen.w / 2)
local halfHeight = math.floor(screen.h / 2)
window.w = halfWidth
window.h = halfHeight
window.x = screen.x
window.y = screen.y + halfHeight
hs.window.focusedWindow():setFrame(window, 0)
end
)
-- 4x4 grid: Bottom right
hs.hotkey.bind({"cmd", "alt"}, "7",
function()
local window, screen = getFocusedWindowAndScreen()
local halfWidth = math.floor(screen.w / 2)
local halfHeight = math.floor(screen.h / 2)
window.w = halfWidth
window.h = halfHeight
window.x = screen.x + halfWidth
window.y = screen.y + halfHeight
hs.window.focusedWindow():setFrame(window, 0)
end
)
-- 16x9 screen recording dimensions
hs.hotkey.bind({"cmd", "alt"}, "0",
function()
local window, screen = getFocusedWindowAndScreen()
local width = 1920
local height = width / (16 / 9)
window.w = width
window.h = height
window.x = screen.x + (screen.w - width)
window.y = screen.y
hs.window.focusedWindow():setFrame(window, 0)
end
)
-- Get the currently focused window and screen
function getFocusedWindowAndScreen()
return
hs.window.focusedWindow():frame(), -- frame of the focused window
hs.window.focusedWindow():screen():frame() -- frame of the focused screen
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment