Auto-open NERDTree in vim

Viewed 22458

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

6 Answers

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()

If you are looking for a way to have a persistent NERDTree, that remains even when you open new tabs, you'd better use jistr/vim-nerdtree-tabs and add in your .vimrc :

let g:nerdtree_tabs_open_on_console_startup=1

The package is not maintained anymore, but it works and I don't know any equivalent.

In your vim config file (I use nvim, so for me its in ~/.config/nvim/init.vim),
Add the line anywhere in the file: au VimEnter * NERDTree

Related