Skip to content

Instantly share code, notes, and snippets.

@huytd
Last active November 16, 2022 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/60b16a270610b5b05248f17fc6f2f2d8 to your computer and use it in GitHub Desktop.
Save huytd/60b16a270610b5b05248f17fc6f2f2d8 to your computer and use it in GitHub Desktop.
An extended ViMouse with y/u/b/n key movement and mouseMove event fix
local vimouse = require('vimouse')
vimouse('cmd', 'm')
-------------------------------------------------------
-- Modal mode for windows movement
hs.window.animationDuration = 0.1
local grid = require('hs.grid')
grid.MARGINX = 0
grid.MARGINY = 0
grid.GRIDHEIGHT = 40
grid.GRIDWIDTH = 60
local movewind = hs.hotkey.modal.new('ctrl-cmd-shift', 'm')
function movewind:entered() hs.alert'Entered moving mode' end
function movewind:exited() hs.alert'Exited moving mode' end
-- Center on the screen
movewind:bind('', 'tab', function()
hs.window.focusedWindow():centerOnScreen()
movewind:exit()
end)
-- Fill to the screen
movewind:bind('', 'space', grid.maximizeWindow)
-- Move to first screen
movewind:bind('', '1', function()
local screens = hs.screen.allScreens()
hs.window.focusedWindow():moveToScreen(screens[1])
end)
-- Move to second screen
movewind:bind('', '2', function()
local screens = hs.screen.allScreens()
hs.window.focusedWindow():moveToScreen(screens[2])
end)
-- Move to third screen
movewind:bind('', '3', function()
local screens = hs.screen.allScreens()
hs.window.focusedWindow():moveToScreen(screens[3])
end)
--- Move window around
movewind:bind('', 'j', grid.pushWindowDown)
movewind:bind('', 'k', grid.pushWindowUp)
movewind:bind('', 'l', grid.pushWindowRight)
movewind:bind('', 'h', grid.pushWindowLeft)
-- Resize window
movewind:bind('ctrl', 'k', grid.resizeWindowShorter)
movewind:bind('ctrl', 'j', grid.resizeWindowTaller)
movewind:bind('ctrl', 'h', grid.resizeWindowThinner)
movewind:bind('ctrl', 'l', grid.resizeWindowWider)
-- Half Left
movewind:bind('alt', 'h', function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrameInScreenBounds(f, 0)
end)
-- Half Right
movewind:bind('alt', 'l', function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + (max.w / 2)
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrameInScreenBounds(f, 0)
end)
-- Top Half
movewind:bind('alt', 'k', function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w
f.h = max.h / 2
win:setFrameInScreenBounds(f, 0)
end)
-- Bottom Half
movewind:bind('alt', 'j', function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y + (max.h / 2)
f.w = max.w
f.h = max.h / 2
win:setFrameInScreenBounds(f, 0)
end)
-- Top Left
movewind:bind('alt', 'y', function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y
f.w = max.w / 2
f.h = max.h / 2
win:setFrameInScreenBounds(f, 0)
end)
-- Top Right
movewind:bind('alt', 'u', function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + (max.w / 2)
f.y = max.y
f.w = max.w / 2
f.h = max.h / 2
win:setFrameInScreenBounds(f, 0)
end)
-- Bottom Left
movewind:bind('alt', 'b', function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x
f.y = max.y + (max.h / 2)
f.w = max.w / 2
f.h = max.h / 2
win:setFrameInScreenBounds(f, 0)
end)
-- Bottom Right
movewind:bind('alt', 'n', function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
f.x = max.x + (max.w / 2)
f.y = max.y + (max.h / 2)
f.w = max.w / 2
f.h = max.h / 2
win:setFrameInScreenBounds(f, 0)
end)
-- Cancel and exit
movewind:bind('', 'escape', '', function()
movewind:exit()
end)
-- Exit and save
movewind:bind('', 'return', '', function()
movewind:exit()
end)
-- Lazy authentication
hs.hotkey.bind({"option"}, "`", function()
local o,s,t,r = hs.execute("security find-generic-password -a masterkey -w")
hs.eventtap.keyStrokes(o)
end)
hs.hotkey.bind({"alt"}, "]", function()
hs.eventtap.event.newSystemKeyEvent('NEXT', true):post()
hs.eventtap.event.newSystemKeyEvent('NEXT', false):post()
end)
hs.hotkey.bind({"alt"}, "[", function()
hs.eventtap.event.newSystemKeyEvent('PREVIOUS', true):post()
hs.eventtap.event.newSystemKeyEvent('PREVIOUS', false):post()
end)
hs.hotkey.bind({"alt"}, "\\", function()
hs.eventtap.event.newSystemKeyEvent('PLAY', true):post()
hs.eventtap.event.newSystemKeyEvent('PLAY', false):post()
end)
-- Expose
expose = hs.expose.new(nil,{
backgroundColor = {0.1, 0.1, 0.1, 0.85},
highlightColor = {0.02, 0.39, 0.92, 1.0},
showTitles = false,
includeNonVisible = false,
fitWindowsMaxIterations = 20,
includeOtherSpaces = false
})
hs.hotkey.bind({"ctrl"}, "m", function()
expose:toggleShow()
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment