How to add NERDTree to your .vimrc
Asked Answered
L

6

129

How do I add NERDTree to my .vimrc?

Loran answered 19/9, 2009 at 0:30 Comment(1)
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 :)Listless
J
214

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
Joappa answered 19/9, 2009 at 14:24 Comment(6)
I dont know why but this does not work for me. I have to call :NERDTreeToggle inside vim to show nerdtree anywayLole
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.Bessbessarabia
You don't need two separate autocmd's: autocmd VimEnter * NERDTree | wincmd pLeu
could I simply specify the actual path I want to start in? - Yup works with a folder (just no slash in the end)Label
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.Archil
'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.Felicitation
G
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
Gunwale answered 9/5, 2012 at 16:4 Comment(0)
L
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.

Listless answered 19/9, 2009 at 3:16 Comment(3)
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...Loran
Thought I'd add that there is a :NERDTreeToggle built in mapping you can map to which makes your custom function rather redundant.Hanukkah
Excellent tip alias vimt='vim -c "NERDTree" $1'Tameika
V
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
Venola answered 27/1, 2013 at 8:51 Comment(0)
V
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
Veiling answered 23/4, 2011 at 8:47 Comment(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'Gaitan
Y
7
" NERD Tree
nmap <silent> <special> <F2> :NERDTreeToggle<RETURN>
Yen answered 10/5, 2012 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.