How to refresh in NERDTree plugin

VimNerdtree

Vim Problem Overview


When I open a file in vim with (Directory A in) NERDTree, it works well.

But if I open one more file in another directory (Directory B), it doesn't refresh to show the contents of directory B (While it still shows directory A).

Can NERDTree automatically refresh by itself?

Vim Solutions


Solution 1 - Vim

From https://gist.github.com/geekontheway/2667442 : just hit the r or R key to refresh the current tree. Could be mapped to auto refresh in .vimrc

Solution 2 - Vim

Keymap to Refresh NERDTree

Instead of switching to the NERDTree window, hitting R and switching back, I use a custom map that does it for me:

nmap <Leader>r :NERDTreeFocus<cr>R<c-w><c-p>

Once set, pressing Leader + r would refresh NERDTree.


Note: Since I also use CtrlP, my actual key map has a last step to refresh CtrlP after refreshing NERDTree

Solution 3 - Vim

After you have opened the new file just issue the :NERDTreeFind command. It will select the current editing file node in the NerdTree. If the node does not exists then the NerdTree will initialize a new tree with the root as the current file's directory.

You can use the autocommand to track the directory while opening vim.

> au VimEnter * NERDTreeFind

Solution 4 - Vim

I detested the idea of having to manually refresh my NERDTree plugin. So, I've added this to my .vimrc:

map <C-n> :call NERDTreeToggleAndRefresh()<CR>

function NERDTreeToggleAndRefresh()
  :NERDTreeToggle
  if g:NERDTree.IsOpen()
    :NERDTreeRefreshRoot
  endif
endfunction

Now, NERDTree refreshes every time I open it.

Solution 5 - Vim

For anyone seeing this on 2016, this worked for me:

autocmd CursorHold,CursorHoldI * call NERDTreeFocus() | call g:NERDTree.ForCurrentTab().getRoot().refresh() | call g:NERDTree.ForCurrentTab().render() | wincmd w

Enjoy!

Solution 6 - Vim

NerdTree will keep pointing at the directory from which vim was originally opened no matter what new files are opened.

In order to change it, place the cursor on the desired directory node inside the NerdTree window and press cd.

NerdTree will confirm the directory change in the command line:

> NERDTree: CWD is now: [new directory here]

Note that this also changes the working directory of vim in general which is important when running commands like :edit somefile.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionToressView Question on Stackoverflow
Solution 1 - VimJohn SmithView Answer on Stackoverflow
Solution 2 - VimSheharyarView Answer on Stackoverflow
Solution 3 - VimsuhairView Answer on Stackoverflow
Solution 4 - VimEric CarlsonView Answer on Stackoverflow
Solution 5 - VimLuis AlejandroView Answer on Stackoverflow
Solution 6 - VimThorsten LorenzView Answer on Stackoverflow