2

I use <bs> for scrolling up half a page and <cr> for scrolling down:

I'd like the same keybindings to be available in NerdTree. It seems that o and <cr> are equivalent in file-mode in NerdTree (just based on the help text) and that <cr> doesn't seems to play much of a role in the other modes:

Is there a way to prevent NerdTree from binding <cr> at all?

More generally, what is a good general strategy (if one exists) for "reclaiming" a keybinding from an overzealous plugin without too many side effects?

1 Answer 1

2

I don't know much about NerdTree, but according to :h NERDTreeKeymapAPI, you can use a function provided by the plugin, NERDTreeAddKeyMap(), to override default mappings:

4.1. Key map API                                           *NERDTreeKeymapAPI*

NERDTreeAddKeyMap({options})                             *NERDTreeAddKeyMap()*
    Adds a new keymapping for all NERD tree buffers.
    {options} must be a dictionary, and must contain the following keys:
    "key" - the trigger key for the new mapping
    "callback" - the function the new mapping will be bound to
    "quickhelpText" - the text that will appear in the quickhelp (see
    |NERDTree-?|)
    "override" - if 1 then this new mapping will override whatever previous
    mapping was defined for the key/scope combo. Useful for overriding the
    default mappings.

    Additionally, a "scope" argument may be supplied. This constrains the
    mapping so that it is only activated if the cursor is on a certain object.
    That object is then passed into the handling method. Possible values are:
        "FileNode" - a file node
        "DirNode" - a directory node
        "Node" - a file or directory node
        "Bookmark" - A bookmark
        "all" - the keymap is not constrained to any scope (default). When
        thei is used, the handling function is not passed any arguments.


    Example: >
        call NERDTreeAddKeyMap({
            \ 'key': 'foo',
            \ 'callback': 'NERDTreeCDHandler',
            \ 'quickhelpText': 'echo full path of current node',
            \ 'scope': 'DirNode' })

        function! NERDTreeCDHandler(dirnode)
            call a:dirnode.changeToDir()
        endfunction
<
    This code should sit in a file like ~/.vim/nerdtree_plugin/mymapping.vim.
    It adds a (redundant) mapping on 'foo' which changes vim's CWD to that of
    the current dir node. Note this mapping will only fire when the cursor is
    on a directory node.

In your case, if you want CR to act as C-d, to scroll down half a page, you could write in ~/.vim/after/plugin/my_nerdtree.vim (you can create the missing directories if ~/.vim/after/plugin/ doesn't exist on your system):

call NERDTreeAddKeyMap({
\                       'key': '<cr>',
\                       'callback': {-> feedkeys("\<c-d>", 'int')},
\                      })

More generally, you can use ~/.vim/after/plugin/my_plugin.vim to override whatever customization was done by a third-party plugin.

So, if NerdTree didn't provide this public function, in the same file as before, you could install an autocmd listening to FileType, which would remap CR to C-d, but only in a NerdTree buffer:

augroup my_nerdtree
    au!
    au FileType nerdtree nnoremap <buffer> <cr> <c-d>
augroup END

Although, both methods are not exactly equivalent here. The public function seems to provide more control over the mapping, and it could even be more reliable, I don't know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.