5.2 KiB
The NERDTree
Introduction
The NERDTree is a file system explorer for the Vim editor. Using this plugin, users can visually browse complex directory hierarchies, quickly open files for reading or editing, and perform basic file system operations.
This plugin can also be extended with custom mappings using a special API. The details of this API and of other NERDTree features are described in the included documentation.
Installation
Below are just some of the methods for installing NERDTree. Do not follow all of these instructions; just pick your favorite one. Other plugin managers exist, and NERDTree should install just fine with any of them.
Vim 8+ packages
If you are using VIM version 8 or higher you can use its built-in package management; see :help packages
for more information. Just run these commands in your terminal:
git clone https://github.com/preservim/nerdtree.git ~/.vim/pack/vendor/start/nerdtree
vim -u NONE -c "helptags ~/.vim/pack/vendor/start/nerdtree/doc" -c q
Otherwise, these are some of the several 3rd-party plugin managers you can choose from. Be sure you read the instructions for your chosen plugin, as there typically are additional steps you nee d to take.
pathogen.vim
In the terminal,
git clone https://github.com/preservim/nerdtree.git ~/.vim/bundle/nerdtree
In your vimrc,
call pathogen#infect()
syntax on
filetype plugin indent on
Then reload vim, run :helptags ~/.vim/bundle/nerdtree/doc/
or :Helptags
.
Vundle.vim
call vundle#begin()
Plugin 'preservim/nerdtree'
call vundle#end()
vim-plug
call plug#begin()
Plug 'preservim/nerdtree'
call plug#end()
dein.vim
call dein#begin()
call dein#add('preservim/nerdtree')
call dein#end()
apt-vim
apt-vim install -y https://github.com/preservim/nerdtree.git
F.A.Q. (here, and in the Wiki)
Is there any support for git
flags?
Yes, install nerdtree-git-plugin.
Can I have the nerdtree on every tab automatically?
Nope. If this is something you want then chances are you aren't using tabs and buffers as they were intended to be used. Read this http://stackoverflow.com/questions/102384/using-vims-tabs-like-buffers
If you are interested in this behaviour then consider vim-nerdtree-tabs
How can I open a NERDTree automatically when vim starts up?
Stick this in your vimrc: autocmd vimenter * NERDTree
How can I open a NERDTree automatically when vim starts up if no files were specified?
Stick this in your vimrc:
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
Note: Now start vim with plain vim
, not vim .
What if I'm also opening a saved session, for example vim -S session_file.vim
? I don't want NERDTree to open in that scenario.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") && v:this_session == "" | NERDTree | endif
How can I open NERDTree automatically when vim starts up on opening a directory?
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | exe 'cd '.argv()[0] | endif
This window is tab-specific, meaning it's used by all windows in the tab. This trick also prevents NERDTree from hiding when first selecting a file.
Note: Executing vim ~/some-directory
will open NERDTree and a new edit window. exe 'cd '.argv()[0]
sets the pwd
of the new edit window to ~/some-directory
How can I map a specific key or shortcut to open NERDTree?
Stick this in your vimrc to open NERDTree with Ctrl+n
(you can set whatever key you want):
map <C-n> :NERDTreeToggle<CR>
How can I close vim if the only window left open is a NERDTree?
Stick this in your vimrc:
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
Can I have different highlighting for different file extensions?
See here: https://github.com/preservim/nerdtree/issues/433#issuecomment-92590696
How can I change default arrows?
Use these variables in your vimrc. Note that below are default arrow symbols
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'
You can remove the arrows altogether by setting these variables to empty strings, as shown below. This will remove not only the arrows, but a single space following them, shifting the whole tree two character positions to the left.
let g:NERDTreeDirArrowExpandable = ''
let g:NERDTreeDirArrowCollapsible = ''