nerdtree

A tree explorer plugin for vim.
Index Commits Files Refs
README.markdown (10395B)
   1 ![Help Wanted](http://blog.ncce.org/wp-content/uploads/2013/12/help-wanted.jpg)
   2 
   3 **NERDTree** is on the lookout for a new maintainer. See [issue #1280](https://github.com/preservim/nerdtree/issues/1280) to submit your name for consideration.
   4 
   5 ---
   6 
   7 # The NERDTree [![Vint](https://github.com/preservim/nerdtree/workflows/Vint/badge.svg)](https://github.com/preservim/nerdtree/actions?workflow=Vint)
   8 
   9 ## Introduction
  10 
  11 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.
  12 
  13 ![NERDTree Screenshot](https://github.com/preservim/nerdtree/raw/master/screenshot.png)
  14 
  15 ## Installation
  16 
  17 Use your favorite plugin manager to install this plugin. [tpope/vim-pathogen](https://github.com/tpope/vim-pathogen), [VundleVim/Vundle.vim](https://github.com/VundleVim/Vundle.vim), [junegunn/vim-plug](https://github.com/junegunn/vim-plug), and [Shougo/dein.vim](https://github.com/Shougo/dein.vim) are some of the more popular ones. A lengthy discussion of these and other managers can be found on [vi.stackexchange.com](https://vi.stackexchange.com/questions/388/what-is-the-difference-between-the-vim-plugin-managers). Basic instructions are provided below, but please **be sure to read, understand, and follow all the safety rules that come with your ~~power tools~~ plugin manager.**
  18 
  19 If you have no favorite, or want to manage your plugins without 3rd-party dependencies, consider using Vim 8+ packages, as described in Greg Hurrell's excellent Youtube video: [Vim screencast #75: Plugin managers](https://www.youtube.com/watch?v=X2_R3uxDN6g).
  20 
  21 <details>
  22 <summary>Pathogen</summary>
  23 Pathogen is more of a runtime path manager than a plugin manager. You must clone the plugins' repositories yourself to a specific location, and Pathogen makes sure they are available in Vim.
  24 
  25 
  26 1. In the terminal,
  27     ```bash
  28     git clone https://github.com/preservim/nerdtree.git ~/.vim/bundle/nerdtree
  29     ```
  30 1. In your `vimrc`,
  31     ```vim
  32     call pathogen#infect()
  33     syntax on
  34     filetype plugin indent on
  35     ```
  36 1. Restart Vim, and run `:helptags ~/.vim/bundle/nerdtree/doc/` or `:Helptags`.
  37 </details>
  38 
  39 <details>
  40   <summary>Vundle</summary>
  41 
  42 1. Install Vundle, according to its instructions.
  43 1. Add the following text to your `vimrc`.
  44     ```vim
  45     call vundle#begin()
  46       Plugin 'preservim/nerdtree'
  47     call vundle#end()
  48     ```
  49 1. Restart Vim, and run the `:PluginInstall` statement to install your plugins.
  50 </details>
  51 
  52 <details>
  53   <summary>Vim-Plug</summary>
  54 
  55 1. Install Vim-Plug, according to its instructions.
  56 1. Add the following text to your `vimrc`.
  57 ```vim
  58 call plug#begin()
  59   Plug 'preservim/nerdtree'
  60 call plug#end()
  61 ```
  62 1. Restart Vim, and run the `:PlugInstall` statement to install your plugins.
  63 </details>
  64 
  65 <details>
  66   <summary>Dein</summary>
  67 
  68 1. Install Dein, according to its instructions.
  69 1. Add the following text to your `vimrc`.
  70     ```vim
  71     call dein#begin()
  72       call dein#add('preservim/nerdtree')
  73     call dein#end()
  74     ```
  75 1. Restart Vim, and run the `:call dein#install()` statement to install your plugins.
  76 </details>
  77 
  78 <details>
  79 <summary>Vim 8+ packages</summary>
  80 
  81 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:
  82 
  83 ```bash
  84 git clone https://github.com/preservim/nerdtree.git ~/.vim/pack/vendor/start/nerdtree
  85 vim -u NONE -c "helptags ~/.vim/pack/vendor/start/nerdtree/doc" -c q
  86 ```
  87 </details>
  88 
  89 ## Getting Started
  90 After installing NERDTree, the best way to learn it is to turn on the Quick Help. Open NERDTree with the `:NERDTree` command, and press `?` to turn on the Quick Help, which will show you all the mappings and commands available in the NERDTree. Of course, your most complete source of information is the documentation: `:help NERDTree`.
  91 
  92 ## NERDTree Plugins
  93 NERDTree can be extended with custom mappings and functions using its built-in API. The details of this API are described in the included documentation. Several plugins have been written, and are available on Github for installation like any other plugin. The plugins in this list are maintained (or not) by their respective owners, and certain combinations may be incompatible.
  94 
  95 * [Xuyuanp/nerdtree-git-plugin](https://github.com/Xuyuanp/nerdtree-git-plugin): Shows Git status flags for files and folders in NERDTree.
  96 * [ryanoasis/vim-devicons](https://github.com/ryanoasis/vim-devicons): Adds filetype-specific icons to NERDTree files and folders,
  97 * [tiagofumo/vim-nerdtree-syntax-highlight](https://github.com/tiagofumo/vim-nerdtree-syntax-highlight): Adds syntax highlighting to NERDTree based on filetype.
  98 * [scrooloose/nerdtree-project-plugin](https://github.com/scrooloose/nerdtree-project-plugin): Saves and restores the state of the NERDTree between sessions.
  99 * [PhilRunninger/nerdtree-buffer-ops](https://github.com/PhilRunninger/nerdtree-buffer-ops): 1) Highlights open files in a different color. 2) Closes a buffer directly from NERDTree.
 100 * [PhilRunninger/nerdtree-visual-selection](https://github.com/PhilRunninger/nerdtree-visual-selection): Enables NERDTree to open, delete, move, or copy multiple Visually-selected files at once.
 101 
 102 If any others should be listed, mention them in an issue or pull request.
 103 
 104 
 105 ## Frequently Asked Questions
 106 
 107 In the answers to these questions, you will see code blocks that you can put in your `vimrc` file.
 108 
 109 ### How can I map a specific key or shortcut to open NERDTree?
 110 
 111 NERDTree doesn't create any shortcuts outside of the NERDTree window, so as not to overwrite any of your other shortcuts. Use the `nnoremap` command in your `vimrc`. You, of course, have many keys and NERDTree commands to choose from. Here are but a few examples.
 112 ```vim
 113 nnoremap <leader>n :NERDTreeFocus<CR>
 114 nnoremap <C-n> :NERDTree<CR>
 115 nnoremap <C-t> :NERDTreeToggle<CR>
 116 nnoremap <C-f> :NERDTreeFind<CR>
 117 ```
 118 
 119 ### How do I open NERDTree automatically when Vim starts?
 120 Each code block below is slightly different, as described in the `" Comment lines`.
 121 
 122 ```vim
 123 " Start NERDTree and leave the cursor in it.
 124 autocmd VimEnter * NERDTree
 125 ```
 126 ---
 127 ```vim
 128 " Start NERDTree and put the cursor back in the other window.
 129 autocmd VimEnter * NERDTree | wincmd p
 130 ```
 131 ---
 132 ```vim
 133 " Start NERDTree when Vim is started without file arguments.
 134 autocmd StdinReadPre * let s:std_in=1
 135 autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif
 136 ```
 137 ---
 138 ```vim
 139 " Start NERDTree. If a file is specified, move the cursor to its window.
 140 autocmd StdinReadPre * let s:std_in=1
 141 autocmd VimEnter * NERDTree | if argc() > 0 || exists("s:std_in") | wincmd p | endif
 142 ```
 143 ---
 144 ```vim
 145 " Start NERDTree, unless a file or session is specified, eg. vim -S session_file.vim.
 146 autocmd StdinReadPre * let s:std_in=1
 147 autocmd VimEnter * if argc() == 0 && !exists('s:std_in') && v:this_session == '' | NERDTree | endif
 148 ```
 149 ---
 150 ```vim
 151 " Start NERDTree when Vim starts with a directory argument.
 152 autocmd StdinReadPre * let s:std_in=1
 153 autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
 154     \ execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif
 155 ```
 156 
 157 ### How can I close Vim or a tab automatically when NERDTree is the last window?
 158 
 159 ```vim
 160 " Exit Vim if NERDTree is the only window remaining in the only tab.
 161 autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
 162 ```
 163 ---
 164 ```vim
 165 " Close the tab if NERDTree is the only window remaining in it.
 166 autocmd BufEnter * if winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
 167 ```
 168 
 169 ### How can I prevent other buffers replacing NERDTree in its window?
 170 
 171 ```vim
 172 " If another buffer tries to replace NERDTree, put it in the other window, and bring back NERDTree.
 173 autocmd BufEnter * if winnr() == winnr('h') && bufname('#') =~ 'NERD_tree_\d\+' && bufname('%') !~ 'NERD_tree_\d\+' && winnr('$') > 1 |
 174     \ let buf=bufnr() | buffer# | execute "normal! \<C-W>w" | execute 'buffer'.buf | endif
 175 ```
 176 
 177 ### Can I have the same NERDTree on every tab automatically?
 178 
 179 ```vim
 180 " Open the existing NERDTree on each new tab.
 181 autocmd BufWinEnter * if &buftype != 'quickfix' && getcmdwintype() == '' | silent NERDTreeMirror | endif
 182 ```
 183 or change your NERDTree-launching shortcut key like so:
 184 ```vim
 185 " Mirror the NERDTree before showing it. This makes it the same on all tabs.
 186 nnoremap <C-n> :NERDTreeMirror<CR>:NERDTreeFocus<CR>
 187 ```
 188 
 189 ### How can I change the default arrows?
 190 
 191 ```vim
 192 let g:NERDTreeDirArrowExpandable = '?'
 193 let g:NERDTreeDirArrowCollapsible = '?'
 194 ```
 195 The preceding values are the non-Windows default arrow symbols. Setting these variables to empty strings will remove the arrows completely and shift the entire tree two character positions to the left. See `:h NERDTreeDirArrowExpandable` for more details.
 196 
 197 ### Can NERDTree access remote files via scp or ftp?
 198 
 199 Short answer: No, and there are no plans to add that functionality. However, Vim ships with a plugin that does just that. It's called netrw, and by adding the following lines to your `.vimrc`, you can use it to open files over the `scp:`, `ftp:`, or other protocols, while still using NERDTree for all local files. The function seamlessly makes the decision to open NERDTree or netrw, and other supported protocols can be added to the regular expression.
 200 
 201 ```vim
 202 " Function to open the file or NERDTree or netrw.
 203 "   Returns: 1 if either file explorer was opened; otherwise, 0.
 204 function! s:OpenFileOrExplorer(...)
 205     if a:0 == 0 || a:1 == ''
 206         NERDTree
 207     elseif filereadable(a:1)
 208         execute 'edit '.a:1
 209         return 0
 210     elseif a:1 =~? '^\(scp\|ftp\)://' " Add other protocols as needed.
 211         execute 'Vexplore '.a:1
 212     elseif isdirectory(a:1)
 213         execute 'NERDTree '.a:1
 214     endif
 215     return 1
 216 endfunction
 217 
 218 " Auto commands to handle OS commandline arguments
 219 autocmd StdinReadPre * let s:std_in=1
 220 autocmd VimEnter * if argc()==1 && !exists('s:std_in') | if <SID>OpenFileOrExplorer(argv()[0]) | wincmd p | enew | wincmd p | endif | endif
 221 
 222 " Command to call the OpenFileOrExplorer function.
 223 command! -n=? -complete=file -bar Edit :call <SID>OpenFileOrExplorer('<args>')
 224 
 225 " Command-mode abbreviation to replace the :edit Vim command.
 226 cnoreabbrev e Edit
 227 ```