Wednesday, May 24, 2017

Hammerspoon

So after upgrading to macOS Sierra one of my most critical utilities Karabiner stopped working.  I used it to map a bunch of shortcut keys on the mac to be the same as windows.  I switch between the two OS's all the time and it drives my muscle memory crazy having to use two different sets of keyboard shortcuts.  Unfortunately, Karabiner isn't supported under sierra.  Karabiner elements doesn't have the features I need like many to many key mappings (mod key combos to different mod key combos).  There is a fork that wwwjfy did that solves the one to many mapping but doesn't solve the man to many mappings.  In the end I completely ditched karabiner elements and uninstalled it.

In comes Hammerspoon and with a little scripting I think I have it all tweaked the way I like it.  My goal was to duplicate this mapping but use Hammerspoon instead:


First thing is to swap the option and command keys (the usual keyboard settings) if you are using an external keyboard.  I do this for all external keyboards but leave the apple internal keyboard as default:

Once you install hammerspoon and enable accessibility,  choose open config.  You should get a blank init.lua file.  Just paste the following.  Leave off the visual studio section if you don't use visual studio for mac.  Then, reload the config.  


-- home/end
hs.hotkey.bind({''}, 'home', nil, function() hs.eventtap.keyStroke({'command'}, 'left')end)
hs.hotkey.bind({"ctrl"}, "home", nil, function() hs.eventtap.keyStroke({"cmd"}, "up")end)
hs.hotkey.bind({""}, "end", nil, function() hs.eventtap.keyStroke({"cmd"}, "right") end)
hs.hotkey.bind({"ctrl"}, "end", nil, function() hs.eventtap.keyStroke({"cmd"}, "down") end)

-- home/end + shift
hs.hotkey.bind('shift','home', nil, function() hs.eventtap.keyStroke({'shift','cmd'},'left')end)
hs.hotkey.bind('shift','end', nil, function() hs.eventtap.keyStroke({'shift','cmd'},'right')end)
hs.hotkey.bind({'shift','ctrl'},'home', nil, function() hs.eventtap.keyStroke({'shift','cmd'},'up')end)
hs.hotkey.bind({'shift','ctrl'},'end', nil, function() hs.eventtap.keyStroke({'shift','cmd'},'down')end)

-- cut/paste
hs.hotkey.bind('ctrl','c',nil,function() hs.eventtap.keyStroke('cmd','c')end)
hs.hotkey.bind('ctrl','v',nil,function() hs.eventtap.keyStroke('cmd','v')end)
hs.hotkey.bind('ctrl','a',nil,function() hs.eventtap.keyStroke('cmd','a')end)
hs.hotkey.bind('ctrl','z',nil,function() hs.eventtap.keyStroke('cmd','z')end)
hs.hotkey.bind('ctrl','x',nil,function() hs.eventtap.keyStroke('cmd','x')end)

--file ops
hs.hotkey.bind('ctrl','s',nil,function() hs.eventtap.keyStroke('cmd','s')end)
hs.hotkey.bind('ctrl','o',nil,function() hs.eventtap.keyStroke('cmd','o')end)
hs.hotkey.bind('ctrl','n',nil,function() hs.eventtap.keyStroke('cmd','n')end)

-- search 
hs.hotkey.bind('ctrl','f',nil,function() hs.eventtap.keyStroke('cmd','f')end)
hs.hotkey.bind('ctrl','g',nil,function() hs.eventtap.keyStroke('cmd','g')end)
hs.hotkey.bind('ctrl','r',nil,function() hs.eventtap.keyStroke('cmd','r')end)
hs.hotkey.bind('','F3',nil,function() hs.eventtap.keyStroke('cmd','g')end)
hs.hotkey.bind('ctrl','F3',nil,function() hs.eventtap.keyStroke({'shift','cmd'},'g')end)

--visual studio
hs.hotkey.bind('ctrl','h',nil,function() hs.eventtap.keyStroke({'alt','cmd'},'f')end)
hs.hotkey.bind('ctrl','.',nil,function() hs.eventtap.keyStroke('alt','return')end)

UPDATE: 11/30/2017
I no longer use hammerspoon.  I highly recommend Better Touch Tool instead.  It's well worth the few dollars.