Created
March 13, 2026 22:24
-
-
Save kcrwfrd/6f3dcaec0e08e0e77b2884588a3456ae to your computer and use it in GitHub Desktop.
Hammerspoon config to move window to a space
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works by clicking on the titlebar of the focused window and then changing spaces with a keyboard shortcut. That means:
I've tested it on Ghostty, Chrome, and Cursor, so I'm happy with it.