Auto-open NERDTree in vim

21,144

Solution 1

 au VimEnter *  NERDTree

in your vimrc should do it

:he autocmd.txt for background

Solution 2

You can also only open Nerd Tree when there was no file on the command line:

function! StartUp()
    if 0 == argc()
        NERDTree
    end
endfunction

autocmd VimEnter * call StartUp()

Taken from a blog post by Ovid.

Solution 3

One liner to open NERDTree when no file argument provided would be

autocmd vimenter * if !argc() | NERDTree | endif
OR
au vimenter * if !argc() | NERDTree | endif

The above code just checks if no argument is provided then open NERDTree.

Solution 4

Building on @zoul's answer, I in my case I wanted NERDTree to be open by default if I specify a directory or if I specify nothing, and not be open if I specify a single file, so I ended up with:

function! StartUp()
    if !argc() && !exists("s:std_in")
        NERDTree
    end
    if argc() && isdirectory(argv()[0]) && !exists("s:std_in")
        exe 'NERDTree' argv()[0]
        wincmd p
        ene
    end
endfunction

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * call StartUp()
Share:
21,144
varnie
Author by

varnie

Updated on July 09, 2022

Comments

  • varnie
    varnie almost 2 years

    Does someone know how to force .vimrc to auto-open NERDTree each time vim is invoked? The operation system is *nix.

  • ereOn
    ereOn almost 12 years
    Very nice tip. Thank you. (I don't know why nobody upvoted that before).
  • Kit Plummer
    Kit Plummer about 4 years
    This should be the accepted solution. It's a PITA when the tree opens when a file is passed in as a CLI argument, and you have to C-WW over every time.