How do I open a file explorer sidebar in Vim with NERDTree?
Answer
:NERDTreeToggle
Explanation
The NERDTree plugin provides a full-featured file explorer sidebar in Vim, giving you a visual directory tree that you can navigate, search, and manipulate files from — similar to the sidebar in VS Code or other modern editors. It remains one of the most popular Vim plugins of all time.
How it works
Toggle the NERDTree sidebar with:
:NERDTreeToggle
This opens a vertical split on the left side of your screen showing the directory tree rooted at your current working directory. Running the command again closes it.
Essential key mappings inside NERDTree
Once the NERDTree window is focused, use these keys to navigate and act on files:
| Key | Action |
|---|---|
o |
Open file/directory (in previous window) |
go |
Preview file (open but keep cursor in NERDTree) |
t |
Open file in a new tab |
T |
Open file in a new tab silently (stay in NERDTree) |
i |
Open file in a horizontal split |
gi |
Preview in horizontal split |
s |
Open file in a vertical split |
gs |
Preview in vertical split |
x |
Close the parent directory node |
X |
Recursively close all child nodes |
p |
Jump to the parent node |
P |
Jump to the root node |
r |
Refresh the current directory |
R |
Refresh the root directory |
m |
Open the file menu (create, rename, move, delete files) |
I |
Toggle hidden files |
? |
Toggle the quick help |
File manipulation menu
Pressing m on any node opens a menu with powerful file operations:
- (a)dd a child node — create a new file or directory (append
/for directories) - (m)ove the current node — rename or relocate a file
- (d)elete the current node — remove a file or directory
- (c)opy the current node — duplicate a file
- (l)ist the current node — show the full path
This means you can create, rename, move, and delete files without ever leaving Vim.
Recommended vimrc configuration
Add a quick toggle mapping and some sensible defaults:
nnoremap <C-n> :NERDTreeToggle<CR>
nnoremap <Leader>nf :NERDTreeFind<CR>
let g:NERDTreeShowHidden = 1 " show hidden/dot files
let g:NERDTreeIgnore = ['\.pyc$', '__pycache__', 'node_modules']
let g:NERDTreeMinimalUI = 1 " hide the help text header
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'
Revealing the current file
The :NERDTreeFind command opens NERDTree and automatically scrolls to and highlights the file you are currently editing. This is incredibly useful for locating a file within a large project tree. Map it for quick access:
nnoremap <Leader>nf :NERDTreeFind<CR>
Auto-close when NERDTree is the last window
Add this autocmd to automatically close Vim when NERDTree is the only remaining window:
autocmd BufEnter * if winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
Tips
- Use
Con a directory to change the tree root to that directory, anduto go up one level - Press
cdon a directory node to change Vim's working directory to that path - NERDTree supports bookmarks: press
Bto toggle the bookmarks list, and:Bookmark nameto save the current node - For a lighter built-in alternative, consider Vim's native
:Explore(netrw) — but NERDTree offers a persistent sidebar and richer file operations - For Neovim users, nvim-tree.lua and neo-tree.nvim provide similar functionality with Lua-native performance and modern features like Git status indicators