Auto-open NERDTree in vim
Asked Answered
H

7

42

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

Heilungkiang answered 18/11, 2009 at 22:57 Comment(0)
F
82
 au VimEnter *  NERDTree

in your vimrc should do it

:he autocmd.txt for background

Franci answered 19/11, 2009 at 0:22 Comment(0)
E
46

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.

Edgardo answered 27/4, 2011 at 6:6 Comment(2)
Very nice tip. Thank you. (I don't know why nobody upvoted that before).Fume
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.Phenacetin
P
10

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.

Passably answered 2/8, 2016 at 11:6 Comment(0)
N
2

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()
Nectareous answered 5/12, 2018 at 15:30 Comment(0)
H
2

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.

Hydrastine answered 8/11, 2021 at 16:1 Comment(0)
L
1

There is an official answer in NERDTree Document.

https://github.com/preservim/nerdtree#how-do-i-open-nerdtree-automatically-when-vim-starts

" Start NERDTree and leave the cursor in it.
autocmd VimEnter * NERDTree

" Start NERDTree and put the cursor back in the other window.
autocmd VimEnter * NERDTree | wincmd p

" Start NERDTree when Vim is started without file arguments.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif

" Start NERDTree. If a file is specified, move the cursor to its window.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * NERDTree | if argc() > 0 || exists("s:std_in") | wincmd p | endif

" Start NERDTree, unless a file or session is specified, eg. vim -S session_file.vim.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists('s:std_in') && v:this_session == '' | NERDTree | endif

" 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
Lathrop answered 7/3, 2023 at 1:47 Comment(0)
Q
0

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

Quadrivium answered 10/1, 2022 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.