Skip to content

Commit

Permalink
Add window movement hotkeys
Browse files Browse the repository at this point in the history
Create a new mode activated with <C-M-S-w> that activates hotkeys to do
window management. Add hotkeys to move windows up, down, left, and
right. One set of hotkeys to move by small increments and another set to
move by slightly larger increments.

Based on code from 'derekwyatt' (https://github.com/derekwyatt):
fikovnik/ShiftIt#296 (comment)

> units = {
>   right30       = { x = 0.70, y = 0.00, w = 0.30, h = 1.00 },
>   right70       = { x = 0.30, y = 0.00, w = 0.70, h = 1.00 },
>   left70        = { x = 0.00, y = 0.00, w = 0.70, h = 1.00 },
>   left30        = { x = 0.00, y = 0.00, w = 0.30, h = 1.00 },
>   top50         = { x = 0.00, y = 0.00, w = 1.00, h = 0.50 },
>   bot50         = { x = 0.00, y = 0.50, w = 1.00, h = 0.50 },
>   upright30     = { x = 0.70, y = 0.00, w = 0.30, h = 0.50 },
>   botright30    = { x = 0.70, y = 0.50, w = 0.30, h = 0.50 },
>   upleft70      = { x = 0.00, y = 0.00, w = 0.70, h = 0.50 },
>   botleft70     = { x = 0.00, y = 0.50, w = 0.70, h = 0.50 },
>   maximum       = { x = 0.00, y = 0.00, w = 1.00, h = 1.00 }
> }
>
> mash = { 'shift', 'ctrl', 'cmd' }
> hs.hotkey.bind(mash, 'l', function() hs.window.focusedWindow():move(units.right30,    nil, true) end)
> hs.hotkey.bind(mash, 'h', function() hs.window.focusedWindow():move(units.left70,     nil, true) end)
> hs.hotkey.bind(mash, 'k', function() hs.window.focusedWindow():move(units.top50,      nil, true) end)
> hs.hotkey.bind(mash, 'j', function() hs.window.focusedWindow():move(units.bot50,      nil, true) end)
> hs.hotkey.bind(mash, ']', function() hs.window.focusedWindow():move(units.upright30,  nil, true) end)
> hs.hotkey.bind(mash, '[', function() hs.window.focusedWindow():move(units.upleft70,   nil, true) end)
> hs.hotkey.bind(mash, ';', function() hs.window.focusedWindow():move(units.botleft70,  nil, true) end)
> hs.hotkey.bind(mash, "'", function() hs.window.focusedWindow():move(units.botright30, nil, true) end)
> hs.hotkey.bind(mash, 'm', function() hs.window.focusedWindow():move(units.maximum,    nil, true) end)
  • Loading branch information
teddywing committed Jun 15, 2019
1 parent f485908 commit 644cdb4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-- Enable command line tool (http://www.hammerspoon.org/docs/hs.ipc.html)
require("hs.ipc")

require('window')


-- Shortcuts to focus tabs in Terminal
terminal_app = hs.application.applicationsForBundleID('com.apple.Terminal')[1]
Expand Down
65 changes: 65 additions & 0 deletions window.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
units = {
up_small = { x = 0, y = -5 },
right_small = { x = 5, y = 0 },
down_small = { x = 0, y = 5 },
left_small = { x = -5, y = 0 },

up_med = { x = 0, y = -20 },
right_med = { x = 20, y = 0 },
down_med = { x = 0, y = 20 },
left_med = { x = -20, y = 0 },

maximum = { x = 0.00, y = 0.00, w = 1.00, h = 1.00 }
}

window = {}
function window.up_small()
hs.window.focusedWindow():move(units.up_small, nil, false, 0)
end

function window.right_small()
hs.window.focusedWindow():move(units.right_small, nil, false, 0)
end

function window.down_small()
hs.window.focusedWindow():move(units.down_small, nil, false, 0)
end

function window.left_small()
hs.window.focusedWindow():move(units.left_small, nil, false, 0)
end

function window.up_med()
hs.window.focusedWindow():move(units.up_med, nil, false, 0)
end

function window.right_med()
hs.window.focusedWindow():move(units.right_med, nil, false, 0)
end

function window.down_med()
hs.window.focusedWindow():move(units.down_med, nil, false, 0)
end

function window.left_med()
hs.window.focusedWindow():move(units.left_med, nil, false, 0)
end


window_mode = hs.hotkey.modal.new({'ctrl', 'option', 'shift'}, 'w', 'Window')
window_mode:bind({'ctrl', 'option', 'shift'}, 'w', 'Window Off', function()
window_mode:exit()
end)

window_mode:bind({'shift'}, 'k', window.up_small, nil, window.up_small)
window_mode:bind({'shift'}, 'l', window.right_small, nil, window.right_small)
window_mode:bind({'shift'}, 'j', window.down_small, nil, window.down_small)
window_mode:bind({'shift'}, 'h', window.left_small, nil, window.left_small)

window_mode:bind({}, 'k', window.up_med, nil, window.up_med)
window_mode:bind({}, 'l', window.right_med, nil, window.right_med)
window_mode:bind({}, 'j', window.down_med, nil, window.down_med)
window_mode:bind({}, 'h', window.left_med, nil, window.left_med)

window_mode:bind({}, ']', nil, nil, function() hs.window.focusedWindow():moveOneScreenEast() end)
window_mode:bind({}, '[', nil, nil, function() hs.window.focusedWindow():moveOneScreenWest() end)

0 comments on commit 644cdb4

Please sign in to comment.