preservim / nerdtree

A tree explorer plugin for vim.
Do What The F*ck You Want To Public License
19.66k stars 1.45k forks source link

What's the remap api for nerdtree menu? #1386

Closed imtheaman closed 1 year ago

imtheaman commented 1 year ago

Hellow awesome people on the internet!

Have to change default m key bindin' in nerdtree, i've bound it to pgup. what's the api for that? i check nerdtree's entirely written in vim script bu' i'm usin' lua configs Thanks, have a good day!

rzvxa commented 1 year ago

Hi,

If I'm not mistaken you want to add custom bindings to the NERDTree menu, Here is a way to do it in Lua:

vim.api.nvim_exec([[
  function! ShortcutCallback()
    let l:currentDirNode = g:NERDTreeDirNode.GetSelected()
    let l:currentNode = g:NERDTreeFileNode.GetSelected()
    echomsg l:currentDirNode.path.str()
    echomsg l:currentNode.path.str()
  endfunction
  autocmd VimEnter * call NERDTreeAddMenuItem({'text': '(t)test menu item', 'shortcut': '<<PageUp>', 'callback': 'ShortcutCallback'})
]], false)

You can also try to use something like this for a cleaner look but I'm not sure if it's going to work or not.

vim.api.nvim_exec([[
  function! ShortcutCallback()
    let l:currentDirNode = g:NERDTreeDirNode.GetSelected()
    let l:currentNode = g:NERDTreeFileNode.GetSelected()
    echomsg l:currentDirNode.path.str()
    echomsg l:currentNode.path.str()
  endfunction
]], false)

vim.fn.NERDTreeAddMenuItem({text = '(t)est menu item', shortcut = 't', callback = 'ShortcutCallback'})

Also, it is possible to write your function in Lua and call it like this:

vim.api.nvim_exec([[
  function! ShortcutCallbackToLua()
    call v:lua.shortcut_callback()
  endfunction
]], false)

function _G.shortcut_callback()
end

vim.fn.NERDTreeAddMenuItem({text = '(t)est menu item', shortcut = 't', callback = 'ShortcutCallbackToLua'})

Binding <PageUp> or any special keys(the ones containing <>) isn't possible in the NERDTree menu as it is getting filtered out by replacing it with <lt> which escapes the special key and makes it a literal string(same as escaping it with a \ character).

imtheaman commented 1 year ago

Thanks @rzvxa