69

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?

Sheharyar
  • 73,588
  • 21
  • 168
  • 215
Toress
  • 1,001
  • 1
  • 9
  • 12

6 Answers6

109

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

Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
John Smith
  • 1,256
  • 1
  • 9
  • 10
11

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

Community
  • 1
  • 1
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • @Jérôme, works perfectly for me. I've been using it for quite a while now; in terminal and in gvim/macvim - both on linux and osx. – Sheharyar Nov 03 '16 at 17:06
  • 1
    It might perform its task, but the \| are superfluous. In fact, these should move the cursor multiple times in the NERDTree window (unless you have a mapping on ``). – Ingo Karkat Nov 03 '16 at 17:29
9

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.

Eric Carlson
  • 129
  • 1
  • 4
5

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

suhair
  • 10,895
  • 11
  • 52
  • 63
2

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!

Luis Alejandro
  • 355
  • 4
  • 5
  • Oy, is there a way to adjust this snippet, so that it remembers the current window, when additional window splits are used? As is, this has the unfortunate side effect of forcing the cursor into a particular location, relative to the split configuration. – mcandre Mar 14 '17 at 02:43
1

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.

Thorsten Lorenz
  • 11,781
  • 8
  • 52
  • 62