Tiago Mendonça
Tiago Mendonça

Reputation: 189

Open nerdtree automatically when start vim on a folder and don't show two tree viewers

I would like to open NERDtree automatically but only when passing a folder argument in command line, and that it would not open two tree viewers, only NERDtree. For example, if I am on folder 'rootFolder', which has a 'childFolder' and I run:

nvim childFolder

I would like it to open vim like if I had done:

cd childFolder
nvim
:NERDtree

And if I open a file or empty I would like it to not show nerdtree. That way if I just work on a single file like:

nvim

or

nvim test.js

or

nvim reminder.txt

I don't need to see NERDtree, because I don't need it, it's just a test script or a notes file I am leaving myself.

Upvotes: 0

Views: 2866

Answers (3)

nibudd
nibudd

Reputation: 56

The previously selected answer didn't work for me, but from NERDTRee's docs, they have a few different solutions depending on your desired behaviour. I think you're looking for the last snippet from the first section:

" Start NERDTree when Vim starts with a directory argument.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
    \ execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif

Upvotes: 0

Tiago Mendonça
Tiago Mendonça

Reputation: 189

I found a workaround, add these two lines in .vimrc:

let g:NERDTreeHijackNetrw = 1
au VimEnter NERD_tree_1 enew | execute 'NERDTree '.argv()[0]

Upvotes: 4

Alexander Batischev
Alexander Batischev

Reputation: 820

That's exactly how NERDTree behaves if 'NERDTreeHijackNetrw' is set to 1 (which it is by default.)

Upvotes: 0

Related Questions