2

I'm trying to remap Nerdtree keys so that <cr> opens a file but the cursor stays in the nerdtree window. Nerdtree has this capability by default and can be triggered with the g + o key mapping. I want this mapping only when in Nerdtree.

From what I understand from the Nerdtree docs the functions this mapping calls is NERDTree-go which is shorthand for 'g' + NERDTreeMapActivateNode

These are what I've tried in my vim rc from a combination of the NerdTree docs and from solutions posed for similar questions.

let NERDTree-go=<cr>

nnoremap <CR> :call nerdtree#ui_glue#invokeKeyMap("go")<CR>

nnoremap <CR> :call nerdtree#invokeKeyMap("go")

let g NERDTreeMapActivateNode = <cr>

let g NERDTree-go

There's also an api method which I think should work but a) it seems like it should be easy to do with a simple remap and b) I can't work out how to set it up. I got as far as getting it loaded but I just had the following in the vimscript file which caused an error:

NERDTreeAddKeyMap({
  'key': '<cr>',
  'callback': 'NERDTree-go',
  'scope': 'FileNode',
  'overide': '1'
})
2

NERDTree normally is configured via g:NERDTreeMap... variables; unfortunately, there's no separate map option for go. From :help NERDTree-go:

The key combo for this mapping is always "g" + NERDTreeMapActivateNode (see NERDTree-o).

So, you have to do the remapping yourself. To apply this only to the NERDTree window, this needs to be a buffer-local mapping, triggered on opening of the NERDTree sidebar:

:autocmd FileType nerdtree nmap <buffer> <CR> go

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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