Search code examples
vimnerdtree

Prevent certain command mappings while in NERDTree window in Vim


I have the following keys mapped in my .vimrc file:

noremap <silent> <C-h> :bprev<CR>
noremap <silent> <C-l> :bnext<CR>

The commands they execute are provided from the buftabs script.

What I would like to do is prevent those key mappings from being executed when I'm in the NERDTree split. The reason for this is if the commands are run while in NERDTree, a file buffer gets loaded in the split instead. Then, to fix it, the window needs to be closed and opened again.

This is a similar problem as explained in another question, but the problem there was corrected by configuring the plugin, while the buftabs script does not have such an option.


Solution

  • In order to disable a mapping in certain buffers, one can define a buffer-local mapping for the same key sequence, overriding the original mapping with a no-op:

    :autocmd FileType nerdtree noremap <buffer> <c-h> <nop>
    :autocmd FileType nerdtree noremap <buffer> <c-l> <nop>
    

    (See :help :map-arguments and :help <nop> for details on <buffer> and <nop>, respectively.)