Skip to content

Instantly share code, notes, and snippets.

@pazimzadeh
Created November 30, 2023 17:16
Show Gist options
  • Save pazimzadeh/b1c70f5f205d0b63264e7c0213ef7afc to your computer and use it in GitHub Desktop.
Save pazimzadeh/b1c70f5f205d0b63264e7c0213ef7afc to your computer and use it in GitHub Desktop.
My Hammerspoon config file
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "R", function()
hs.reload()
end)
hs.alert.show("Config loaded")
hs.loadSpoon("ShiftIt")
spoon.ShiftIt:bindHotkeys({})
spoon.ShiftIt:bindHotkeys({
left = {{ 'alt', 'cmd' }, 'z' },
right = {{ 'alt', 'cmd' }, 'x' },
up = {{ 'alt', 'cmd' }, 'q' },
down = {{ 'alt', 'cmd' }, 'a' },
resizeIn = {{ 'alt', 'cmd' }, '-' },
resizeOut = {{ 'alt', 'cmd' }, '=' },
toggleZoom = {{ 'alt', 'cmd' }, 'f' },
upleft = {{ 'alt', 'cmd' }, '1' },
upright = {{ 'alt', 'cmd' }, '2' },
botleft = {{ 'alt', 'cmd' }, '3' },
botright = {{ 'alt', 'cmd' }, '4' }
})
hs.hotkey.bind({"cmd", "alt"}, "s", function()
-- size focused window to middle third of display
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + (max.w / 3)
f.y = max.y
f.w = max.w / 3
f.h = max.h
win:setFrame(f)
end)
-- move window to next screen
-- bind hotkey
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, ']', function()
-- get the focused window
local win = hs.window.focusedWindow()
-- get the screen where the focused window is displayed, a.k.a. current screen
local screen = win:screen()
-- compute the unitRect of the focused window relative to the current screen
-- and move the window to the next screen setting the same unitRect
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
end)
-- move window to previous screen
-- bind hotkey
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, '[', function()
-- get the focused window
local win = hs.window.focusedWindow()
-- get the screen where the focused window is displayed, a.k.a. current screen
local screen = win:screen()
-- compute the unitRect of the focused window relative to the current screen
-- and move the window to the previous screen setting the same unitRect
win:move(win:frame():toUnitRect(screen:frame()), screen:previous(), true, 0)
end)
@pazimzadeh
Copy link
Author

New version:

hs.hotkey.bind({"cmd", "alt", "ctrl"}, "R", function()
  hs.reload()
end)
hs.alert.show("Config loaded")

hs.loadSpoon("ShiftIt")
spoon.ShiftIt:bindHotkeys({})

spoon.ShiftIt:bindHotkeys({
  left = {{ 'alt', 'cmd' }, 'z' },
  right = {{ 'alt', 'cmd' }, 'x' },
  up = {{ 'alt', 'cmd' }, 'e' },
  down = {{ 'alt', 'cmd' }, 'd' },
  resizeIn = {{ 'alt', 'cmd' }, '-' },
  resizeOut = {{ 'alt', 'cmd' }, '=' },
  --toggleZoom = {{ 'alt', 'cmd' }, 'f' },
  upleft = {{ 'alt', 'cmd' }, 'q' },
  upright = {{ 'alt', 'cmd' }, 'w' },
  botleft = {{ 'alt', 'cmd' }, 'a' },
  botright = {{ 'alt', 'cmd' }, 's' }
})

-- START Focus toggle (maximize, fullscreen, or return to previous size)
local windowStates = {}  -- To store original window frames
local cycleIndex = 1     -- To track the current cycle state

local function cycleWindowFocus()
  local win = hs.window.focusedWindow()
  if not win then return end
  
  -- Define the actions in the cycle
  local actions = {
    function()  -- First action: Save original size and maximize
      windowStates[win:id()] = win:frame()  -- Store the current frame
      win:maximize()
    end,
    function()  -- Second action: Go fullscreen
      win:setFullScreen(true)
    end,
    function()  -- Third action: Restore original size and exit fullscreen
      win:setFullScreen(false)
      local originalFrame = windowStates[win:id()]
      if originalFrame then
        win:setFrame(originalFrame)
      end
    end
  }

  -- Perform the current action
  actions[cycleIndex]()

  -- Move to the next action in the cycle
  cycleIndex = cycleIndex % #actions + 1
end

-- Bind the cycle function to a shortcut (e.g., cmd + alt + F)
hs.hotkey.bind({"cmd", "alt"}, "f", cycleWindowFocus)
-- END Focus toggle


hs.hotkey.bind({"cmd", "alt"}, "1", function()
  -- size focused window to middle third of display
  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 / 3
  f.h = max.h
  win:setFrame(f)
end)

--[[
hs.hotkey.bind({"cmd", "alt"}, "2", function()
  -- size focused window to middle third of display
  local win = hs.window.focusedWindow()
  local f = win:frame()
  local screen = win:screen()
  local max = screen:frame()

  f.x = max.x + (max.w / 3)
  f.y = max.y
  f.w = max.w / 3
  f.h = max.h
  win:setFrame(f)
end)
--]]

hs.hotkey.bind({"cmd", "alt"}, "4", function()
  -- size focused window to right third of display
  local win = hs.window.focusedWindow()
  local f = win:frame()
  local screen = win:screen()
  local max = screen:frame()

  f.x = max.x + (2 * max.w / 3)
  f.y = max.y
  f.w = max.w / 3
  f.h = max.h
  win:setFrame(f)
end)

hs.hotkey.bind({"cmd", "alt"}, "2", function()
  -- size focused window to left two-thirds of display
  local win = hs.window.focusedWindow()
  local f = win:frame()
  local screen = win:screen()
  local max = screen:frame()

  f.x = max.x  -- Start at the left edge of the screen
  f.y = max.y
  f.w = (2 * max.w) / 3  -- Set width to two-thirds of the screen
  f.h = max.h
  win:setFrame(f)
end)

hs.hotkey.bind({"cmd", "alt"}, "3", function()
  -- size focused window to right two-thirds of display
  local win = hs.window.focusedWindow()
  local f = win:frame()
  local screen = win:screen()
  local max = screen:frame()

  f.x = max.x + (max.w / 3)  -- Start at one-third of the screen width
  f.y = max.y
  f.w = (2 * max.w) / 3  -- Set width to two-thirds of the screen
  f.h = max.h
  win:setFrame(f)
end)

-- Define the action to move the window to the next screen
local function moveToNextScreen()
  -- get the focused window
  local win = hs.window.focusedWindow()
  -- get the screen where the focused window is displayed, a.k.a. current screen
  local screen = win:screen()
  -- compute the unitRect of the focused window relative to the current screen
  -- and move the window to the next screen setting the same unitRect 
  win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
end

-- Bind multiple shortcuts to the same action
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, ']', moveToNextScreen)
hs.hotkey.bind({'alt', 'cmd'}, '.', moveToNextScreen)
hs.hotkey.bind({'ctrl', 'alt'}, 'x', moveToNextScreen)

-- Define the action to move the window to the previous screen
local function moveToPreviousScreen()
  -- get the focused window
  local win = hs.window.focusedWindow()
  -- get the screen where the focused window is displayed, a.k.a. current screen
  local screen = win:screen()
  -- compute the unitRect of the focused window relative to the current screen
  -- and move the window to the next screen setting the same unitRect 
  win:move(win:frame():toUnitRect(screen:frame()), screen:previous(), true, 0)
end

hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, '[', moveToPreviousScreen)
hs.hotkey.bind({'ctrl', 'alt'}, 'z', moveToPreviousScreen)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment