Create a gist now

Instantly share code, notes, and snippets.

@ar-tama /init.lua
Last active Jan 30, 2017

my hammerspoon config
-- remap functions
local function keyCode(key, mods, callback)
mods = mods or {}
callback = callback or function() end
return function()
hs.eventtap.event.newKeyEvent(mods, string.lower(key), true):post()
hs.timer.usleep(1000)
hs.eventtap.event.newKeyEvent(mods, string.lower(key), false):post()
callback()
end
end
local function remapKey(mods, key, keyCode)
hs.hotkey.bind(mods, key, keyCode, nil, keyCode)
end
local function killLine()
return keyCode("right", {"cmd", "shift"}, keyCode("x", {"cmd"}))
end
-- watch & switch hotkey settings
local function switchHotKeys(enable)
for k, v in pairs(hs.hotkey.getHotkeys()) do
if enable then
v["_hk"]:enable()
else
v["_hk"]:disable()
end
end
end
local function handleGlobalEvent(name, event, app)
if event == hs.application.watcher.activated then
if name == "Emacs" or name == "iTerm2" then
switchHotKeys(false)
else
switchHotKeys(true)
end
end
end
watcher = hs.application.watcher.new(handleGlobalEvent)
watcher:start()
-- remap settings
remapKey({"ctrl"}, "p", keyCode("up"))
remapKey({"ctrl"}, "n", keyCode("down"))
remapKey({"ctrl"}, "f", keyCode("right"))
remapKey({"ctrl"}, "b", keyCode("left"))
remapKey({"ctrl"}, "m", keyCode("return"))
remapKey({"ctrl"}, "j", keyCode("return"))
remapKey({"ctrl"}, "w", keyCode("x", {"cmd"}))
remapKey({"ctrl"}, "y", keyCode("v", {"cmd"}))
remapKey({"ctrl"}, "h", keyCode("delete"))
remapKey({"ctrl"}, "k", killLine())
remapKey({"ctrl"}, "a", keyCode("left", {"cmd"}))
remapKey({"ctrl"}, "e", keyCode("right", {"cmd"}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment