Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example hydra for nvim tree #77

Open
JoseConseco opened this issue Feb 16, 2023 · 0 comments
Open

Example hydra for nvim tree #77

JoseConseco opened this issue Feb 16, 2023 · 0 comments

Comments

@JoseConseco
Copy link

JoseConseco commented Feb 16, 2023

Not sure if there is place to share custom hydras. Here is one for nvim-tree plugin. The way it works - as soon as you open or leave nvim-tree panel, the hydra window will be shown/hidden automatically. This way I do not have to remember the keys - in video below I used hydra to rename file:

nvim-tree_hydra.webm
There is no need for special key to close hydra, since as soon as user closes, leaves Nvim-tree the hydra is automatically closed.

local api = require("nvim-tree.api")

local hint = [[
 _w_: cd CWD   _c_: Copy File Path      _/_: Filter
 _y_: Copy     _x_: Cut                 _p_: Paste
 _r_: Rename   _d_: Remove              _n_: New
 _h_: Hidden   _?_: Help
 ^
]]
-- ^ ^           _q_: exit

local nvim_tree_hydra = nil
local nt_au_group = vim.api.nvim_create_augroup("NvimTreeHydraAU", { clear = true })

local function close_nvim_tree_hydra()
  if nvim_tree_hydra then
    nvim_tree_hydra:exit()
  end
end

Hydra.spawn = function(head)
  if head == "nvim_tree_hydra" then
    nvim_tree_hydra = Hydra({
      name = "NvimTree",
      hint = hint,
      config = {
        color = "pink",
        invoke_on_body = true,
        buffer = 0,  -- only for active buffer
        hint = {
          position = "bottom",
          border = "rounded",
        },
      },
      mode = "n",
      body = "H",
      heads = {
        { "w", change_root_to_global_cwd, { silent = true } },
        { "c", api.fs.copy.absolute_path, { silent = true } },
        { "/", api.live_filter.start, { silent = true } },
        { "y", api.fs.copy.node, { silent = true } },
        { "x", api.fs.cut, { exit = true, silent = true } },
        { "p", api.fs.paste, { exit = true, silent = true } },
        { "r", api.fs.rename, { silent = true } },
        { "d", api.fs.remove, { silent = true } },
        { "n", api.fs.create, { silent = true } },
        { "h", api.tree.toggle_hidden_filter, { silent = true } },
        { "?", api.tree.toggle_help, { silent = true } },
        -- { "q", nil, { exit = true, nowait = true } },
      },
    })

    nvim_tree_hydra:activate()

  end
end

vim.api.nvim_create_autocmd({ "BufEnter" }, {
	pattern = "*" ,
	callback = function(opts)
        if vim.bo[opts.buf].filetype == "NvimTree" then
          Hydra.spawn["nvim_tree_hydra"]()
        else
          close_nvim_tree_hydra() -- does not unload the keys - but hiding hydra ok...
        end    
    group = nt_au_group,
})
@JoseConseco JoseConseco changed the title Exaple hydra for nvim tree Example hydra for nvim tree Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant