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?

Bamqf
  • 3,382
  • 8
  • 33
  • 47
  • 1
    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](http://vimcasts.org/blog/2013/01/oil-and-vinegar-split-windows-and-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](https://github.com/tpope/vim-projectionist) fill my needs without wasting any space or opening up annoying splits – Peter Rincker Jul 18 '14 at 04:42
  • Possible duplicate of [Make NERDtree not be the default window when VIM starts?](https://stackoverflow.com/questions/31253080/make-nerdtree-not-be-the-default-window-when-vim-starts) – linesarefuzzy Sep 09 '17 at 19:06

2 Answers2

47

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
dave
  • 62,300
  • 5
  • 72
  • 93
  • Thanks, it works! By the way, does your one-liner omit a `p` in the end? I get an error without it. – Bamqf Jul 17 '14 at 16:55
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

lovelikelando
  • 7,593
  • 6
  • 32
  • 50
  • You forgot the default action. Full line should be `autocmd VimEnter * if argc() == 1 | NERDTree | wincmd p | else | NERDTree | endif`. – George Aristy Aug 25 '18 at 19:24