Skip to content

Instantly share code, notes, and snippets.

@prenagha
Created September 30, 2016 00:36
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save prenagha/1c28f71cb4d52b3133a4bff1b3849c3e to your computer and use it in GitHub Desktop.
Save prenagha/1c28f71cb4d52b3133a4bff1b3849c3e to your computer and use it in GitHub Desktop.
Hammerspoon Config File, Hyper Key, Karabiner-Elements
-- hattip https://github.com/lodestone/hyper-hacks
-- hattip https://gist.github.com/ttscoff/cce98a711b5476166792d5e6f1ac5907
-- A global variable for the sub-key Hyper Mode
k = hs.hotkey.modal.new({}, 'F18')
-- Hyper+key for all the below are setup somewhere
-- The handler already exists, usually in Keyboard Maestro
-- we just have to get the right keystroke sent
hyperBindings = {'c','m','a','r','d','g','s','f','TAB','v','b'}
for i,key in ipairs(hyperBindings) do
k:bind({}, key, nil, function() hs.eventtap.keyStroke({'cmd','alt','shift','ctrl'}, key)
k.triggered = true
end)
end
-- Enter Hyper Mode when F19 (left control) is pressed
pressedF19 = function()
k.triggered = false
k:enter()
end
-- Leave Hyper Mode when F19 (left control) is pressed,
-- send ESCAPE if no other keys are pressed.
releasedF19 = function()
k:exit()
if not k.triggered then
hs.eventtap.keyStroke({}, 'ESCAPE')
end
end
-- Bind the Hyper key
f19 = hs.hotkey.bind({}, 'F19', pressedF19, releasedF19)
-- vi cursor movement commands
movements = {
{ 'h', {}, 'LEFT'},
{ 'j', {}, 'DOWN'},
{ 'k', {}, 'UP'},
{ 'l', {}, 'RIGHT'},
{ '0', {'cmd'}, 'LEFT'}, -- beginning of line
{ '4', {'cmd'}, 'RIGHT'}, -- end of line
{ 'b', {'alt'}, 'LEFT'}, -- back word
{ 'w', {'alt'}, 'RIGHT'}, -- forward word
}
for i,bnd in ipairs(movements) do
hs.hotkey.bind({'ctrl'}, bnd[1], function()
hs.eventtap.keyStroke(bnd[2],bnd[3])
end)
end
-- Reload config when any lua file in config directory changes
function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == '.lua' then
doReload = true
end
end
if doReload then
hs.reload()
end
end
local myWatcher = hs.pathwatcher.new(os.getenv('HOME') .. '/.hammerspoon/', reloadConfig):start()
hs.alert.show('Config loaded')
@ssaadh
Copy link

ssaadh commented Oct 3, 2016

Thanks for this. Liked seeing a bit of different config here. Get me grokking Hammerspoon and even Lua a bit more.

@theReticent
Copy link

Awesome. My muscle memory is thanking you! But isn't there a way to use hjkl with the hyper key instead of control? And this way you have to release ctrl+j after each stroke which is a bit tedious.

@clickysteve
Copy link

Thank you for your work on this! I forked and uploaded my tweaked code for my own setup here:

https://gist.github.com/clickysteve/e13a11b8fc9c963c08b3109d95bbacc5

Gave attribution but not sure of the correct etiquette, so let me know if anything should be revised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment