Last active
January 21, 2024 18:23
-
-
Save ar-tama/213f2022bf9d1c42790b37c6b605dab5 to your computer and use it in GitHub Desktop.
my hammerspoon config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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