Skip to content

Commit

Permalink
swap hammerspoon configs from yabai to aerospace
Browse files Browse the repository at this point in the history
Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Sep 25, 2024
1 parent be743ab commit 65d63e8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 86 deletions.
7 changes: 3 additions & 4 deletions config/aerospace/aerospace.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ alt-6 = 'workspace 6'
alt-7 = 'workspace 7'
alt-8 = 'workspace 8'
alt-9 = 'workspace 9'
#alt-t = 'workspace T'
#alt-t = 'workspace T' # hammerspoon bind F1

# See: https://nikitabobko.github.io/AeroSpace/commands#move-node-to-workspace
alt-shift-1 = ['move-node-to-workspace 1', 'workspace 1']
Expand Down Expand Up @@ -151,13 +151,12 @@ alt-shift-j = ['join-with down', 'mode main']
alt-shift-k = ['join-with up', 'mode main']
alt-shift-l = ['join-with right', 'mode main']

# TODO: focus back with bind for:
# aerospace focus-back-and-forth || aerospace workspace-back-and-forth
# Focus binds through hammerspoon
# https://nikitabobko.github.io/AeroSpace/commands#focus

[[on-window-detected]]
if.app-id = 'org.alacritty'
run = 'move-node-to-workspace 1'
run = 'move-node-to-workspace T'

[[on-window-detected]]
if.app-id = 'com.tinyspeck.slackmacgap'
Expand Down
99 changes: 17 additions & 82 deletions config/hammerspoon/init.lua
Original file line number Diff line number Diff line change
@@ -1,93 +1,28 @@
mash = {"", "", ""}

local function toggleApp(name)
function aerospace(args)
hs.task.new("/opt/homebrew/bin/aerospace", function(ud, ...)
hs.inspect(table.pack(...))
return true
end, args):start()
end


local function activateApp(name)
local app = hs.application.find(name)
if not app or app:isHidden() then
hs.application.launchOrFocus(name)
elseif hs.application.frontmostApplication() ~= app then
app:activate()
else
-- NB: does not bring back fullscreen if we were fullscreen..
-- Could maybe write a dedicated toggler that remembers fullscreen, but don't care that much.
app:hide()
-- unfortunately we cannot swap out easily with aerospace
-- hiding the window would mean an empty workspace or pulling in another window
-- thus we hardcode the toggle to go back to 3 for now
aerospace({"workspace", "3"})
end
end

-- Global terminal toggle
hs.hotkey.bind({}, "F1", function() toggleApp("alacritty") end)

-- Reload yabai
hs.hotkey.bind(mash, "r", function() hs.reload(); os.execute("/opt/homebrew/bin/yabai --restart-service"); end)
hs.alert("HS + yabai loaded")

--function bindCmd(key, cmd)
-- hs.hotkey.bind({"shift", "alt"}, key, function() os.execute(cmd) end)
--end

-- Yabai
local function yabai(commands)
for _, cmd in ipairs(commands) do
os.execute("/opt/homebrew/bin/yabai -m " .. cmd)
end
end
-- bind on alt/shift only
--
-- For adopters: my binds ends up being shift-alt on a windows keyboard
-- ..but that is AFTER remapping Control and Command on the keyboard globally in mac settings
-- change these two binds to something that works
-- (modifier switch + ctrl intercepted elsewhere in e.g. alacritty)
local function alt(key, commands)
hs.hotkey.bind({ "alt" }, key, function()
yabai(commands)
end)
end
local function shiftAlt(key, commands)
hs.hotkey.bind({ "shift", "alt" }, key, function()
yabai(commands)
end)
end


hs.hotkey.bind({ "shift", "alt" }, "m", function()
hs.execute("~/.config/yabai/focus.sh", true)
end)

-- window moving between monitors/spaces
for i = 1, 9 do
local num = tostring(i)
alt(num, { "space --focus " .. num })
shiftAlt(num, { "window --space " .. num, "space --focus " .. num })
end
--shiftAlt("tab", { "space --focus recent" })

-- NOTE: hjkl -> neio in colemak - use as arrow keys
local homeRow = { n = "west", e = "south", i = "north", o = "east" }

for key, direction in pairs(homeRow) do
alt(key, { "window --focus " .. direction })
shiftAlt(key, { "window --swap " .. direction })
end

-- window resize
shiftAlt("down", { "window --resize bottom:0:20" }) -- increase down
shiftAlt("right", { "window --resize right:20:0" }) -- increase right
shiftAlt("up", { "window --resize bottom:0:-20" }) -- decrease down
shiftAlt("left", { "window --resize right:-20:0" }) -- decrease right

-- mode toggles for spaces and windows
--shiftAlt("y", { "space --layout bsp" })
--shiftAlt("u", { "space --layout stack" })
--shiftAlt(";", { "space --layout float" }) --probably only want to do this for windows only?
alt("f", { "window --toggle zoom-fullscreen" })
shiftAlt("f", { "window --toggle native-fullscreen" })
alt("l", { "window --toggle float", "window --grid 8:10:1:1:8:6" }) -- unfloat/float + center
shiftAlt("l", { "window --toggle topmost", "window --grid 5:5:4:1:4:4" }) -- pin cornered on a ws

-- NB: alt-f only works on mac if you keep modifiers in the same position

-- mini pip window, note; does not retain properties correctly on yabai restart
--shiftAlt("p", { "window --toggle pip", "window --toggle float", "window --toggle sticky", "window --toggle topmost" })
--shiftAlt("v", { "space --toggle padding", "space --toggle gap" })

-- layout experiments
shiftAlt("r", { "space --rotate 90" })
-- Global terminal activation
hs.hotkey.bind({}, "F1", function() activateApp("alacritty") end)
hs.hotkey.bind(mash, "r", function() hs.reload(); aerospace("reload-config"); end)
hs.alert("HS + aerospace loaded")
96 changes: 96 additions & 0 deletions config/hammerspoon/yabai.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
mash = {"", "", ""}

-- yabai variant. of init.lua
-- worked great, but not using it atm due to SIP not being unlockable now.

local function toggleApp(name)
local app = hs.application.find(name)
if not app or app:isHidden() then
hs.application.launchOrFocus(name)
elseif hs.application.frontmostApplication() ~= app then
app:activate()
else
-- NB: does not bring back fullscreen if we were fullscreen..
-- Could maybe write a dedicated toggler that remembers fullscreen, but don't care that much.
app:hide()
end
end

-- Global terminal toggle
hs.hotkey.bind({}, "F1", function() toggleApp("alacritty") end)

-- Reload yabai
hs.hotkey.bind(mash, "r", function() hs.reload(); os.execute("/opt/homebrew/bin/yabai --restart-service"); end)
hs.alert("HS + yabai loaded")

--function bindCmd(key, cmd)
-- hs.hotkey.bind({"shift", "alt"}, key, function() os.execute(cmd) end)
--end

-- Yabai
local function yabai(commands)
for _, cmd in ipairs(commands) do
os.execute("/opt/homebrew/bin/yabai -m " .. cmd)
end
end
-- bind on alt/shift only
--
-- For adopters: my binds ends up being shift-alt on a windows keyboard
-- ..but that is AFTER remapping Control and Command on the keyboard globally in mac settings
-- change these two binds to something that works
-- (modifier switch + ctrl intercepted elsewhere in e.g. alacritty)
local function alt(key, commands)
hs.hotkey.bind({ "alt" }, key, function()
yabai(commands)
end)
end
local function shiftAlt(key, commands)
hs.hotkey.bind({ "shift", "alt" }, key, function()
yabai(commands)
end)
end


hs.hotkey.bind({ "shift", "alt" }, "m", function()
hs.execute("~/.config/yabai/focus.sh", true)
end)

-- window moving between monitors/spaces
for i = 1, 9 do
local num = tostring(i)
alt(num, { "space --focus " .. num })
shiftAlt(num, { "window --space " .. num, "space --focus " .. num })
end
--shiftAlt("tab", { "space --focus recent" })

-- NOTE: hjkl -> neio in colemak - use as arrow keys
local homeRow = { n = "west", e = "south", i = "north", o = "east" }

for key, direction in pairs(homeRow) do
alt(key, { "window --focus " .. direction })
shiftAlt(key, { "window --swap " .. direction })
end

-- window resize
shiftAlt("down", { "window --resize bottom:0:20" }) -- increase down
shiftAlt("right", { "window --resize right:20:0" }) -- increase right
shiftAlt("up", { "window --resize bottom:0:-20" }) -- decrease down
shiftAlt("left", { "window --resize right:-20:0" }) -- decrease right

-- mode toggles for spaces and windows
--shiftAlt("y", { "space --layout bsp" })
--shiftAlt("u", { "space --layout stack" })
--shiftAlt(";", { "space --layout float" }) --probably only want to do this for windows only?
alt("f", { "window --toggle zoom-fullscreen" })
shiftAlt("f", { "window --toggle native-fullscreen" })
alt("l", { "window --toggle float", "window --grid 8:10:1:1:8:6" }) -- unfloat/float + center
shiftAlt("l", { "window --toggle topmost", "window --grid 5:5:4:1:4:4" }) -- pin cornered on a ws

-- NB: alt-f only works on mac if you keep modifiers in the same position

-- mini pip window, note; does not retain properties correctly on yabai restart
--shiftAlt("p", { "window --toggle pip", "window --toggle float", "window --toggle sticky", "window --toggle topmost" })
--shiftAlt("v", { "space --toggle padding", "space --toggle gap" })

-- layout experiments
shiftAlt("r", { "space --rotate 90" })

0 comments on commit 65d63e8

Please sign in to comment.