A better NerdTree setup

Jun 4, 2015 · 2 min read

NerdTree is one of the first plugins you’ll install in Vim. It allows you to browse your files easily and perform some basic operations like creating or moving files without leaving vim.

It’s useful enough to take a minute to set it up properly, yet I often see developers not fully taking advantage of it. Here are a couple of tips to make your NerdTree more awesome:

Open by default

One of the first things I often do when opening a project is taking a look at the file tree to refresh my memory or get the sense of what is happening in a new project. Rather than opening NerdTree manually each time you want to do that, here’s what you can add to your vimrc to open NerdTree when you’re starting vim with no command line arguments (taken from NerdTree’s Readme):

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists(“s:std_in”) | NERDTree | endif

Finding the right file

You probably already have something similar to this in your .vimrc to open and close NerdTree:

nnoremap <Leader>f :NERDTreeToggle<Enter>

However, you can also directly open NerdTree on the file you’re editing to quickly perform operations on it with NERDTreeFind. Here’s how I recommend adding it to your vimrc:

nnoremap <silent> <Leader>v :NERDTreeFind<CR>

I also noticed that a lot of people forget that they’re still in vim when navigating NerdTree. Use / search, gg and G liberally to move around faster.

Git integration

It’s always good to know what files you’ve modified since your last commit, and Xuyuanp’s nerdtree-git-plugin shows exactly that. You’ll have to make sure you update NerdTree to its latest version, but it’s totally worth it.

Closing automatically

My favorite vim configs are the ones that save you keystrokes and reduce the noise in your workflow by automatically doing the things you need to do anyways. This is one of them: automatically close NerdTree when you open a file:

let NERDTreeQuitOnOpen = 1

And here is how to automatically close a tab if the only remaining window is NerdTree (also taken from the Readme):

autocmd bufenter * if (winnr(“$”) == 1 && exists(“b:NERDTreeType”) && b:NERDTreeType == “primary”) | q | endif

Deleting files

Automatically delete the buffer of the file you just deleted with NerdTree:

let NERDTreeAutoDeleteBuffer = 1

Making it prettier

You’re going to be looking at your NerdTree a lot. You might as well make sure it looks nice and disable that old “Press ? for help”.

let NERDTreeMinimalUI = 1
let NERDTreeDirArrows = 1

If you found these useful, make sure you check out the rest of my vim config, and steal whatever you like.

Happy editing!

Victor Mours

Written by

Software engineer. Organizer of the #ParisRubyWorkshop

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade