logoalt Hacker News

zdwyesterday at 7:55 PM5 repliesview on HN

I fake a tiling window manager on Mac with Hammerspoon, resizing to fit in specific corners/sizes:

     -- resize based on ratios
    function ratioResize(xr, yr, wr, hr)
      return function ()
        local win = hs.window.focusedWindow()
        win:moveToUnit({x=xr,y=yr,w=wr,h=hr})
      end
    end

    -- 4 corners, different sizes
    hs.hotkey.bind({"cmd", "ctrl"}, "w", ratioResize(0,     0, 2/5, 2/3))
    hs.hotkey.bind({"cmd", "ctrl"}, "e", ratioResize(2/5,   0, 3/5, 2/3))
    hs.hotkey.bind({"cmd", "ctrl"}, "s", ratioResize(0,   2/3, 2/5, 1/3))
    hs.hotkey.bind({"cmd", "ctrl"}, "d", ratioResize(2/5, 2/3, 3/5, 1/3))
And to throw windows to other monitors:

    -- send to next screen
    hs.hotkey.bind({"cmd", "ctrl"}, ";", function()
      local win = hs.window.focusedWindow()
      local screen = win:screen()
      local next_screen = screen:next()

      win:moveToScreen(next_screen)
    end)

Replies

comboyyesterday at 8:24 PM

I highly recommend Aerospace[1], went through a few approaches, I cared about not completely compromising security either, it works really well if you come from something like i3

1. https://github.com/nikitabobko/AeroSpace

show 1 reply
jeberleyesterday at 8:43 PM

I use it similarly, but I add spots for side x side as well as left, center, right. I only use Hammerspoon for this and a couple tiny things, but it's completely worth it for this alone. Use math to specify window sizes & location. Insanity.

  local mode = hs.screen.primaryScreen():currentMode()
  local mods = {"ctrl", "alt", "cmd"}  -- mash those keys
  
  -- regular app windows
  do
    local w   = 1094  -- no clip on GitHub, HN
    local h   = 1122  -- tallish
    local x_1 =    0                               -- left edge
    local x_2 = math.max(0, (mode.w - w - w) / 2)  -- left middle
    local x_3 =             (mode.w - w) / 2       -- middle
    local x_4 = math.min(mode.w - w, x_2 + w + 1)  -- right middle
    local x_5 =          mode.w - w                -- right edge
    local y   =   23  -- top of screen below menu bar
  
    hs.hotkey.bind(mods, "2", function() move_win(  0, y, mode.w, mode.h) end)  -- max
  
    hs.hotkey.bind(mods, "3", function() move_win(x_1, y, w, h) end)
    hs.hotkey.bind(mods, "4", function() move_win(x_2, y, w, h) end)
    hs.hotkey.bind(mods, "5", function() move_win(x_3, y, w, h) end)
    hs.hotkey.bind(mods, "6", function() move_win(x_4, y, w, h) end)
    hs.hotkey.bind(mods, "7", function() move_win(x_5, y, w, h) end)
  end
  
  function move_win(x, y, w, h)
    hs.window.focusedWindow():setFrame(hs.geometry.rect(x, y, w, h))
  end
ackfoobaryesterday at 11:06 PM

https://github.com/peterklijn/hammerspoon-shiftit

I use ShiftIt (a lovely project, but dead) reimplemented in Hammerspoon. It is very comprehensive.

apazzoliniyesterday at 9:33 PM

I like the miro windows manager plugin: https://github.com/miromannino/miro-windows-manager

It's nice to be able to iterate through the halves/thirds configurations for different cases.

piskovyesterday at 8:52 PM

Moom is really the best