I'm attempting to remap 'ctrl-w' to 'alt-delete' and 'alt-d' to
'alt-forwarddelete' globally, but disable that binding in Emacs and Terminal.
(I'll caveat the below by saying that I'm not a programmer, just a simple
dotfile copypasta hack.)
The below code works well, but with 2 problems:
1. When switching from Emacs to Terminal (or Terminal to Emacs) the bindings
are disabled, then immediately re-enabled. Is this a bug or a problem with
my code? (probably code)
2. I would love to be able to group both key bindings and enable them as a set
(in reality I have 9 individual bindings that I'm using this technique
for), is that possible?
3. It seems like I should be able to create a single IF Emacs OR Terminal
statement but similar problems to the below approach persist.
Any help would be appreciated!
-----
function deleteWordBack()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, true):post()
hs.eventtap.event.newKeyEvent('delete', true):post()
hs.eventtap.event.newKeyEvent('delete', false):post()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, false):post()
end
function deleteWordForward()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, true):post()
hs.eventtap.event.newKeyEvent('forwarddelete', true):post()
hs.eventtap.event.newKeyEvent('forwarddelete', false):post()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, false):post()
end
local bindDeleteWordBack = hs.hotkey.bind({'ctrl'}, 'w', deleteWordBack)
local bindDeleteWordForward = hs.hotkey.bind({'alt'}, 'd', deleteWordForward)
bindDeleteWordBack:enable()
bindDeleteWordForward:enable()
appKeyBinder =
hs.application.watcher.new(function(appName, eventType, appObject)
if appName == "Terminal" then
if eventType == hs.application.watcher.activated then
bindDeleteWordBack:disable()
bindDeleteWordForward:disable()
elseif eventType == hs.application.watcher.deactivated or eventType == hs.application.watcher.terminated then
bindDeleteWordBack:enable()
bindDeleteWordForward:enable()
end
elseif appName == "Emacs" then
if eventType == hs.application.watcher.activated then
bindDeleteWordBack:disable()
bindDeleteWordForward:disable()
elseif eventType == hs.application.watcher.deactivated or eventType == hs.application.watcher.terminated then
bindDeleteWordBack:enable()
bindDeleteWordForward:enable()
end
end
end):start()