hydra.nvim
hydra.nvim copied to clipboard
How to use Hydra without define keymap in `head`
I'm migrating to hydra from which-key. With the later, I just need define key name and description inside which-key module without having to defining keybinding inside. Is it possible with hydra
. Put nil
in head's rhs doesn't work because keymap is not activated :(
seems you just need to bind a no-op to the body. this config might help
invoke_on_body = true,
on_enter = function() end,
well, i misread, and now i really dont know what do you want. if you dont want to bind a head, what makes you think you need a hydra?
@babygau can you please provide what particular you want to do with an example? Because now I don't really get what's the problem.
@haolian9 and @anuvyklack thank you for your response. I'm very sorry if my question is not clear enough. Let's say I wanna make a Telescope keybinding to find files with <Leader>ff
. If I use Hydra, this should be:
Hydra({
name = 'Telescope',
config = {
color = 'teal',
invoke_on_body = true,
},
mode = 'n',
body = '<Leader>f',
heads = {
{ 'f', cmd 'Telescope find_files' },
{ '<Enter>', cmd 'Telescope', { exit = true, desc = 'list all pickers' } },
{ '<Esc>', nil, { exit = true, nowait = true } },
}
})
However, I don't want to define keybinding inside Hydra but in other module, so I want something like
Hydra({
name = 'Telescope',
config = {
color = 'teal',
invoke_on_body = true,
},
mode = 'n',
body = '<Leader>f',
heads = {
{ 'f', nil },
{ '<Enter>', cmd 'Telescope', { exit = true, desc = 'list all pickers' } },
{ '<Esc>', nil, { exit = true, nowait = true } },
}
})
-- In `telescope.lua` module
vim.keymap.set({"n"}, "<Leader>ff", "<Cmd>Telescope find_files<CR>", {noremap = true, nowait = true, silent = true})
But if I do as the above example, press <Leader>ff
do nothing.
You can consider hydra is a one big keybinding, that's bonded to <Leader>f
. You also have another <Leader>ff
keymap that invoke Telescope file-finder. So when you press <Leader>f
Vim will wait timeoutlen
(see :help timeoutlen
) seconds for continuation (since you have another longer keymap with the same prefix) and activate hydra. The hydra binds its heads something like (for f
head): <Plug>(Hydra2)f
and it has nothing in common with <Leader>ff>
. The <Leader>f
is another keymap, that only activates the hydra. But you can bind one keymap to another using remap
keymap option:
Hydra({
...
mode = 'n',
body = '<Leader>f',
heads = {
{ 'f', '<Leader>ff', { remap = true } },
...
}
})
-- In `telescope.lua` module
-- No need for `noremap = true` here, since it is the default.
vim.keymap.set({"n"}, "<Leader>ff", "<Cmd>Telescope find_files<CR>")