NERDTree is a file explorer plugin for Vim that lets you browse your project’s file structure from within your editor.

Here’s how to set it up and use it:

First, you need a plugin manager. If you don’t have one, I recommend vim-plug. Install it by following the instructions on its GitHub page.

Once vim-plug is installed, add the following line to your ~/.vimrc file:

Plug 'preservim/nerdtree'

After saving your ~/.vimrc, open Vim and run the command :PlugInstall. This will download and install NERDTree.

Now, let’s configure NERDTree to open automatically when you start Vim, but only if you’re opening a directory. Add these lines to your ~/.vimrc:

autocmd vimenter * if !argc() | NERDTree | else | wincmd p | endif
autocmd BufEnter * if winnr('$') > 1 && getbufvar(bufnr('%'), '&buftype') =~# 'quickfix\|help\|[Nn]erdtree' | wincmd p | endif

The first autocmd opens NERDTree when Vim starts without any files open (i.e., when you open Vim in a directory). If Vim is started with files, it will move the cursor to the previously active window. The second autocmd prevents NERDTree from stealing focus if you open a quickfix or help window.

To toggle NERDTree manually, you can map a key. A common choice is F5. Add this to your ~/.vimrc:

nnoremap <F5> :NERDTreeToggle<CR>

Now, when you press F5 in normal mode, NERDTree will appear or disappear.

Let’s look at the NERDTree interface. When it opens, you’ll see your current directory’s structure.

/home/user/myproject/
├── .git/
├── src/
│   ├── __init__.py
│   └── main.py
├── tests/
│   └── test_main.py
└── README.md

Here are some essential keybindings within the NERDTree window:

  • j / k: Move the cursor down/up.
  • h / l: Collapse/expand the current directory.
  • o: Open the file/directory under the cursor in the current window.
  • v: Open the file in a vertical split.
  • t: Open the file in a new tab.
  • i: Open the file in a horizontal split.
  • d: Delete the file/directory.
  • R: Rename the file/directory.
  • a: Add a new file.
  • D: Delete a file or directory without confirmation.
  • ?: Show the help menu with all keybindings.

You can customize the appearance of NERDTree. For example, to show hidden files (those starting with .) by default, add this to your ~/.vimrc:

let g:NERDTreeShowHidden = 1

If you want to hide specific directories or file types, you can use g:NERDTreeIgnore. For instance, to ignore all .pyc files and the .git directory:

let g:NERDTreeIgnore = ['\.pyc$', '^.git$']

NERDTree also supports bookmarks. You can create a bookmark for a specific directory by navigating to it in NERDTree and pressing mB (for "mark bookmark"). You can then list all bookmarks with :NERDTree bookmarks.

The most surprising thing about NERDTree’s configuration is how many of its behaviors are controlled by simple boolean flags or string arrays in your .vimrc. You can extensively customize its look and feel, from what files are displayed to how they are opened, without needing to dive into the plugin’s source code. For instance, setting let g:NERDTreeDirArrows = 1 will add little arrows next to directories, a purely cosmetic change that many users find helpful for visual scanning.

Once you’re comfortable with NERDTree, you’ll likely want to explore other file-related plugins that integrate with it, such as vim-fugitive for Git integration.

Want structured learning?

Take the full Vim course →