Skip to content

Instantly share code, notes, and snippets.

@kcrwfrd
Created March 13, 2026 22:24
Show Gist options
  • Select an option

  • Save kcrwfrd/6f3dcaec0e08e0e77b2884588a3456ae to your computer and use it in GitHub Desktop.

Select an option

Save kcrwfrd/6f3dcaec0e08e0e77b2884588a3456ae to your computer and use it in GitHub Desktop.
Hammerspoon config to move window to a space
local function moveWindowToSpace(spaceNumber)
local win = hs.window.focusedWindow()
if not win then
hs.alert.show("No focused window")
return
end
local frame = win:frame()
local titleBarPoint = {
x = frame.x + frame.w / 2,
y = frame.y + 3
}
local originalPos = hs.mouse.absolutePosition()
hs.mouse.absolutePosition(titleBarPoint)
local steps = {
{ delay = 0.05, fn = function()
hs.eventtap.event.newMouseEvent(
hs.eventtap.event.types.leftMouseDown, titleBarPoint):post()
end },
{ delay = 0.15, fn = function()
hs.eventtap.event.newKeyEvent({"ctrl"}, tostring(spaceNumber), true):post()
hs.eventtap.event.newKeyEvent({"ctrl"}, tostring(spaceNumber), false):post()
end },
{ delay = 0.4, fn = function()
hs.eventtap.event.newMouseEvent(
hs.eventtap.event.types.leftMouseUp, hs.mouse.absolutePosition()):post()
end },
{ delay = 0.01, fn = function()
hs.mouse.absolutePosition(originalPos)
end },
}
local function runStep(i)
if i > #steps then return end
hs.timer.doAfter(steps[i].delay, function()
steps[i].fn()
runStep(i + 1)
end)
end
runStep(1)
end
for i = 0, 9 do
hs.hotkey.bind({"cmd", "alt", "ctrl", "shift"}, tostring(i), function()
local target = i == 0 and 10 or i
moveWindowToSpace(target)
end)
end
@kcrwfrd
Copy link
Copy Markdown
Author

kcrwfrd commented Mar 13, 2026

This works by clicking on the titlebar of the focused window and then changing spaces with a keyboard shortcut. That means:

  • It requires keyboard shortcuts bound to change spaces
  • Only works for spaces that are on the same display as the window you are trying to move
  • May not work for windows with atypical title bars

I've tested it on Ghostty, Chrome, and Cursor, so I'm happy with it.

@JIANSUJIAN
Copy link
Copy Markdown

works great. Thanks!

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