130

How do I add NERDTree to my .vimrc?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
chutsu
  • 13,612
  • 19
  • 65
  • 86
  • I updated my answer. I'm not sure you can add it to your .vimrc to do what you want, but I know you can run vim from the command line in a way that opens up NERDTree automatically. Take a look :) – Steven Kryskalla Sep 19 '09 at 13:09

6 Answers6

215

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Douglas Mayle
  • 21,063
  • 9
  • 42
  • 57
  • I dont know why but this does not work for me. I have to call :NERDTreeToggle inside vim to show nerdtree anyway – hgf Dec 09 '10 at 15:57
  • If you're using a script like vimpager you should move these lines to your `/.vim/after/plugin/NERD_tree.vim` script, so that it doesn't try to execute with plugins turned off. – Joshua Olson Apr 23 '11 at 17:51
  • 10
    You don't need two separate autocmd's: `autocmd VimEnter * NERDTree | wincmd p` – Dave James Miller Apr 21 '12 at 22:03
  • could I simply specify the actual path I want to start in? - Yup works with a folder (just no slash in the end) – AturSams Sep 15 '14 at 12:56
  • 3
    I prefer to do the following: `autocmd VimEnter * NERDTree` and then `autocmd VimEnter * if argc() | wincmd p | endif`. That way, the cursor is on NERDTree if I haven't opened a specific file, but if I did pick a file it starts in the main window. – user2275806 Apr 15 '15 at 15:20
  • 'm trying to setup up NERDTree with neovim 0.4.4 (release) but it's not working. I copied the autocmd code snippets from the projects github page. – Caesar23 Jul 29 '21 at 21:21
24

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
gsf
  • 1,778
  • 16
  • 11
20

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.

Steven Kryskalla
  • 14,179
  • 2
  • 40
  • 42
  • I'm running a Unix-y machine. I have NERDTree Installed, what I need is to have NERDTree to start when I type vim in the commandline. So that a file browser always opens to the left, like in Textmate. I don't know what to put into the vimrc to do this, I tried :NERDTree but it does not seem to recognise the command... – chutsu Sep 19 '09 at 09:28
  • 2
    Thought I'd add that there is a :NERDTreeToggle built in mapping you can map to which makes your custom function rather redundant. – Gavin Gilmour Sep 19 '09 at 13:54
  • 1
    Excellent tip `alias vimt='vim -c "NERDTree" $1'` – pedrosaurio Jun 04 '13 at 17:59
9

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
TenJack
  • 1,594
  • 4
  • 21
  • 35
7
" NERD Tree
nmap <silent> <special> <F2> :NERDTreeToggle<RETURN>
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
7

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
Mikel
  • 24,855
  • 8
  • 65
  • 66
  • 1
    Good One ... I also use Vim as a pager and simply added an argument to disable autocommands ... `export MANPAGER='col -bx | mvim -c ":set ft=man nonu nolist" -c ":autocmd!" -M -R - > /dev/null 2>&1'` – Edward J Beckett Jun 19 '12 at 02:58