Skip to content

Instantly share code, notes, and snippets.

@shmup
Created January 12, 2026 17:39
Show Gist options
  • Select an option

  • Save shmup/8458236e012d061063ec4841498252d1 to your computer and use it in GitHub Desktop.

Select an option

Save shmup/8458236e012d061063ec4841498252d1 to your computer and use it in GitHub Desktop.
; Window positioning script with hotkeys for moving/resizing windows
; Supports half-screen, corner, maximize, and center positioning
; Repeat a snap hotkey to toggle compact mode for that snap family.
; User settings
center_x_padding := 400
center_y_padding := 200
compact_scale := 0.85
; State
compact_mode := false
last_snap_key := ""
GetMonitorDimensions(&monitorLeft, &monitorTop, &monitorWidth, &monitorHeight) {
; Use largest-overlap selection to robustly determine the monitor
WinGetPos(&wx, &wy, &ww, &wh, "A")
bestArea := -1
bestIndex := 1
monitorCount := MonitorGetCount()
Loop monitorCount {
MonitorGet(A_Index, &ml, &mt, &mr, &mb)
ow := Max(0, Min(wx + ww, mr) - Max(wx, ml))
oh := Max(0, Min(wy + wh, mb) - Max(wy, mt))
area := ow * oh
if (area > bestArea) {
bestArea := area
bestIndex := A_Index
}
}
; Fallback to primary monitor for minimized/zero-overlap cases
if (bestArea <= 0) {
bestIndex := 1
}
MonitorGetWorkArea(bestIndex, &wl, &wt, &wr, &wb)
monitorLeft := wl
monitorTop := wt
monitorWidth := wr - wl
monitorHeight := wb - wt
}
GetMonitorRect() {
monLeft := 0, monTop := 0, monWidth := 0, monHeight := 0
GetMonitorDimensions(&monLeft, &monTop, &monWidth, &monHeight)
return { left: monLeft, top: monTop, width: monWidth, height: monHeight }
}
GetWindowFrameOffsets(hwnd, &offLeft, &offTop, &offRight, &offBottom) {
offLeft := 0, offTop := 0, offRight := 0, offBottom := 0
if (!hwnd) {
return false
}
WinGetPos(&wx, &wy, &ww, &wh, "ahk_id " hwnd)
rect := Buffer(16, 0)
if (DllCall("dwmapi\DwmGetWindowAttribute", "ptr", hwnd, "int", 9, "ptr", rect, "int", 16) != 0) {
return false
}
frLeft := NumGet(rect, 0, "int")
frTop := NumGet(rect, 4, "int")
frRight := NumGet(rect, 8, "int")
frBottom := NumGet(rect, 12, "int")
offLeft := frLeft - wx
offTop := frTop - wy
offRight := (wx + ww) - frRight
offBottom := (wy + wh) - frBottom
return true
}
MoveWindowTo(x, y, w, h, winTitle := "A") {
hwnd := WinExist(winTitle)
offLeft := 0, offTop := 0, offRight := 0, offBottom := 0
if (GetWindowFrameOffsets(hwnd, &offLeft, &offTop, &offRight, &offBottom)) {
x := x - offLeft
y := y - offTop
w := w + offLeft + offRight
h := h + offTop + offBottom
}
WinMove(x, y, w, h, "ahk_id " hwnd)
}
ApplyCompactMode(x, y, w, h, alignX, alignY, shrinkX, shrinkY) {
if (!compact_mode) {
return { x: x, y: y, w: w, h: h }
}
newW := shrinkX ? (w * compact_scale) : w
newH := shrinkY ? (h * compact_scale) : h
if (alignX = "left") {
newX := x
} else if (alignX = "right") {
newX := x + (w - newW)
} else {
newX := x + (w - newW) / 2
}
if (alignY = "top") {
newY := y
} else if (alignY = "bottom") {
newY := y + (h - newH)
} else {
newY := y + (h - newH) / 2
}
return { x: newX, y: newY, w: newW, h: newH }
}
HandleSnap(key, x, y, w, h, alignX, alignY, shrinkX, shrinkY) {
global compact_mode, last_snap_key
if (last_snap_key = key) {
compact_mode := !compact_mode
} else {
last_snap_key := key
}
rect := ApplyCompactMode(x, y, w, h, alignX, alignY, shrinkX, shrinkY)
MoveWindowTo(rect.x, rect.y, rect.w, rect.h)
}
; half screens
^#h:: {
rect := GetMonitorRect()
HandleSnap("h", rect.left, rect.top, rect.width / 2, rect.height, "left", "top", true, false)
}
^#j:: {
rect := GetMonitorRect()
HandleSnap("j", rect.left, rect.top + (rect.height / 2), rect.width, rect.height / 2, "left", "bottom", false, true)
}
^#k:: {
rect := GetMonitorRect()
HandleSnap("k", rect.left, rect.top, rect.width, rect.height / 2, "left", "top", false, true)
}
^#l:: {
rect := GetMonitorRect()
HandleSnap("l", rect.left + (rect.width / 2), rect.top, rect.width / 2, rect.height, "right", "top", true, false)
}
; corners
^#y:: {
rect := GetMonitorRect()
HandleSnap("y", rect.left, rect.top, rect.width / 2, rect.height / 2, "left", "top", true, true)
}
^#u:: {
rect := GetMonitorRect()
HandleSnap("u", rect.left + (rect.width / 2), rect.top, rect.width / 2, rect.height / 2, "right", "top", true, true)
}
^#b:: {
rect := GetMonitorRect()
HandleSnap("b", rect.left, rect.top + (rect.height / 2), rect.width / 2, rect.height / 2, "left", "bottom", true, true)
}
^#n:: {
rect := GetMonitorRect()
HandleSnap("n", rect.left + (rect.width / 2), rect.top + (rect.height / 2), rect.width / 2, rect.height / 2, "right", "bottom", true, true)
}
; maximized (not fullscreen)
^#m:: {
rect := GetMonitorRect()
HandleSnap("m", rect.left, rect.top, rect.width, rect.height, "center", "center", true, true)
}
; centered with padding
^#c:: {
rect := GetMonitorRect()
width := rect.width - (center_x_padding * 2)
height := rect.height - (center_y_padding * 2)
HandleSnap("c", rect.left + center_x_padding, rect.top + center_y_padding, width, height, "center", "center", true, true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment