Skip to content

Instantly share code, notes, and snippets.

@ar-tama
Last active January 21, 2024 18:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ar-tama/213f2022bf9d1c42790b37c6b605dab5 to your computer and use it in GitHub Desktop.
Save ar-tama/213f2022bf9d1c42790b37c6b605dab5 to your computer and use it in GitHub Desktop.
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