vim: open NERDTree and move the cursor to the file editing area
Asked Answered
A

2

26

I tried following the instruction in FAQ section on NERDTree github site:

"Q. How can I open a NERDTree automatically when vim starts up?"

"A. Stick this in your vimrc: autocmd vimenter * NERDTree"

It works but when I open a file the cursor stay in the NEARDTree explorer area but not in the edit area, I have to press Ctrl+w+l to move it back, what should I write in my .vimrc file to automate setting the cursor in the edit area?

Aeonian answered 17/7, 2014 at 16:30 Comment(2)
Alternatively don't use NerdTree or more specifically avoid the drawer. Here is a nice Vimcast article: Oil and vinegar - split windows and the project drawer. Personally I rarely have a need for the use of a file explorer. I find simple tab completion, fuzzy finders, ctags/cscope, and Tim Pope's Projectionist fill my needs without wasting any space or opening up annoying splitsGrimy
Possible duplicate of Make NERDtree not be the default window when VIM starts?Mcatee
M
49

Just add this second command right after:

autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p

Or if you want a one-liner

autocmd VimEnter * NERDTree | wincmd p
Mabellemable answered 17/7, 2014 at 16:35 Comment(1)
Thanks, it works! By the way, does your one-liner omit a p in the end? I get an error without it.Aeonian
D
2

If you want to keep the default behavior if no file is specified (i.e. leave the NERDTree explorer only if there is a file to edit) you can use this setting:

autocmd VimEnter * if argc() == 1 | NERDTree | wincmd p | endif

Demitasse answered 7/12, 2016 at 16:51 Comment(1)
You forgot the default action. Full line should be autocmd VimEnter * if argc() == 1 | NERDTree | wincmd p | else | NERDTree | endif.Crew

© 2022 - 2024 — McMap. All rights reserved.