How to add NERDTree to your .vimrc

VimNerdtree

Vim Problem Overview


How do I add NERDTree to my .vimrc?

Vim Solutions


Solution 1 - Vim

Okay, the previous version was a bit terse, but the answer you're looking for is to add the line below into your ~/.vimrc file. It tells Vim that you want to setup a command to run when Vim starts, but since it depends on various plugins to be loaded, you don't want to run it until all initialization is finished:

autocmd VimEnter * NERDTree

If, however, you're annoyed by the fact that the cursor always starts in the NERDTree window, you can add a second autocommand that will move the cursor into the main window:

autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p

Solution 2 - Vim

I like to see NERDTree only when I start vim without file arguments, so I added this to my .vimrc:

autocmd VimEnter * if !argc() | NERDTree | endif

Solution 3 - Vim

Are you on a Windows or unix-y system?

If you're on a unix-y system you put plugins in ~/.vim/plugin. Here's what my plugin directory looks like:

$ ls ~/.vim/plugin
NERD_tree.vim  scratch.vim  scratchfind.vim

After that it starts working right away. Try running vim like this:

$ vim .

It should open the current directory in the NERD tree view.

If you're on Windows you put plugins here: C:\Program Files\Vim\vim70\plugin


To get NERDTree to load automatically when you start up vim, run it like this from the command line:

$ vim -c "NERDTree" some_file.txt

You can set an alias for this in your .bashrc:

alias vimt='vim -c "NERDTree" $1'

Now whenever you run vimt (instead of vim) you'll also open up NERDTree on the left side of the window.

You could also add a shortcut key to start NERDTree in your .vimrc this way:

function OpenNERDTree()
  execute ":NERDTree"
endfunction
command -nargs=0 OpenNERDTree :call OpenNERDTree()

nmap <ESC>t :OpenNERDTree<CR>

Now when you hit Esc then t it will pop open NERDTree.

Solution 4 - Vim

Per the NERDTree instructions you can just use pathogen.vim. Install it with:

mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
        https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim

Add this to your .vimrc:

execute pathogen#infect()

then install NERDTree:

cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git

And if you want to open a NERDTree automatically when Vim starts up, add the following to your .vimrc:

autocmd vimenter * NERDTree

Solution 5 - Vim

The answers here have a minor problem.

If you call vim --noplugin or use a script that uses --noplugin mode such as vimpager, it will cause this error:

Error detected while processing VimEnter Auto commands for "*":
E492: Not an editor command: NERDTree

To avoid this, put the command in ~/.vim/after/plugin/NERD_tree.vim instead:

autocmd VimEnter * NERDTree

And it might also be a good idea to test that NERDtree is available as well, i.e.:

if exists("loaded_nerd_tree")
    autocmd VimEnter * NERDTree
endif

Solution 6 - Vim

" NERD Tree
nmap <silent> <special> <F2> :NERDTreeToggle<RETURN>

Solution 7 - Vim

I like to open NERDTree on startup, but with two requirements:

  • Open NERDTree only when there are no arguments (file arguments)
  • Display only the NERDTree window (I don't want to display the main window)

I use this command:

autocmd VimEnter * if !argc() | NERDTree | wincmd p | q | endif

Update (9 Jan 2022)

I've found a more performant way to meet the two requirements I've specified above.

Remove the autocmd, which I've mentioned above, from the .vimrc file. Instead, create an alias like this:

vim() {
  if [ $# -eq 0 ]; then
    /usr/bin/vim ./
  else
    /usr/bin/vim "$@"
  fi
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchutsuView Question on Stackoverflow
Solution 1 - VimDouglas MayleView Answer on Stackoverflow
Solution 2 - VimgsfView Answer on Stackoverflow
Solution 3 - VimSteven KryskallaView Answer on Stackoverflow
Solution 4 - VimTenJackView Answer on Stackoverflow
Solution 5 - VimMikelView Answer on Stackoverflow
Solution 6 - VimSergioAraujoView Answer on Stackoverflow
Solution 7 - VimadamgerhartzView Answer on Stackoverflow