3

I discovered Hammerspoon today, a keybinding and desktop automation tool reminiscent of i3wm or awesonewm for linux, but combined with something like xdotool.

I am an emacs user, and find emacs's (and similarly vim's) solution of using "prefix" keys to support more keybindings useful.

In emacs (and vim) you can support "prefix" keys, which once pressed select a different map of keybindings. So for example:

C-x followed by , followed by v could be bound to a single keybinding which is different from v.

Is there any way to acheive this in hammerspoon (short of writing my own library that toggles bindings on and off as I press keys)?

Research

Att Righ
  • 382

1 Answers1

2

If you're still wondering how to do this, then the RecursiveBinder Spoon for Hammerspoon will do the trick. I wrote a Medium article about how to use it to make a leader key for macOS.

Here is a minimal lua snippet on how to use it.

hs.loadSpoon("RecursiveBinder")  -- Load the spoon

spoon.RecursiveBinder.escapeKey = {{}, 'escape'} -- Press escape to abort

-- singleKey is a convenience function provided in the Spoon local singleKey = spoon.RecursiveBinder.singleKey

local keyMap = { [singleKey('b', 'browser')] = function() hs.application.launchOrFocus("Firefox") end, -- 'b' is the keybinding, 'browser' is the text shown in the onscreen helper text [singleKey('t', 'terminal')] = function() hs.application.launchOrFocus("Terminal") end, [singleKey('d', 'domain+')] = { -- Separate layer [singleKey('g', 'github')] = function() hs.urlevent.openURL("github.com") end, [singleKey('y', 'youtube')] = function() hs.urlevent.openURL("youtube.com") end } }

-- Calling recursiveBind returns the activation function, and here, it is bound to option+space hs.hotkey.bind({'option'}, 'space', spoon.RecursiveBinder.recursiveBind(keyMap))

Here, pressing option+space activates the first layer with b, t, d (browser, terminal, domain respectively) Pressing d will activate the layer with g and y.

At each level, a helper text is shown, like which-key in emacs.

NethumL
  • 21