Vim - How to position cursor automatically on first file or the file I am coming from in a (secondary) NerdTreeBuffer?

StackOverflow https://stackoverflow.com/questions/14278754

  •  15-01-2022
  •  | 
  •  

Question

I haven't managed to figure out how to position the cursor on fourth/fifth line corresponding to the first file entry in a NerdTree buffer openend via :ex . (NERDTreeHijackNetrw=1).

Following line in my .vimrc doesn't work:

autocmd BufEnter * silent! if (exists("b:NERDTreeType") && b:NERDTreeType == "secondary") | <missing command> | endif
Was it helpful?

Solution

I've been trying it in several ways, but the problem is that all of NERDTrees inner methods are private (they are defined with an s: infront of them, making it hard to re-use them), and also that hooking the BufEnter event is not very useful, because NERDTree does not seem to have been fully initialized yet when that event is fired.

At the moment, I think this would be the best solution:

fun! NewTreeOpen()
    NERDTreeFind
    wincmd o
    let g:NERDTreeQuitOnOpen=1
endfun

What this does is:

  • Opens a new NERDTree sidebar window with your current file selected
  • Makes the NERDTree window the only window
  • Tells NERDTree to close the (full-sized) sidebar again after you have selected your file

I hope that is good enough. Otherwise I think you should do a feature request to the author of NERDTree to ask if he can add this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top