70

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?

3
  • 43
    If anyone else came here trying to find out how to refresh the list of files under a directory, just select the directory node and press r.
    – James M
    May 26, 2012 at 17:14
  • @JamesMcLaughlin Add that as an answer. Will upvote. Jul 22, 2013 at 21:37
  • possible duplicate of NERDTree reload new files May 19, 2015 at 7:10

6 Answers 6

111

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

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

2
  • @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 3, 2016 at 17:06
  • 1
    It might perform its task, but the <space>\|<space> are superfluous. In fact, these should move the cursor multiple times in the NERDTree window (unless you have a mapping on <Space>). Nov 3, 2016 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.

4
  • This should be the accepted answer. Works perfectly
    – Alex Mckay
    Jan 24, 2021 at 1:13
  • Great, thanks for sharing! I'm now using this - with the addition that I use :NERDTreeFind if there is an active buffer.
    – steinar
    May 6, 2022 at 11:05
  • I expect this to make NERDTree hang for minutes if root has a lot of childs (like / or such) Dec 20, 2022 at 16:51
  • You may be right. That was not a use case I had to concern myself with since I only use VIM in project folders. Dec 21, 2022 at 19:35
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

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!

1
  • 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, 2017 at 2: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.

1
  • 1
    It can be automated with this setting: let NERDTreeChDirMode=2
    – romainl
    Dec 21, 2011 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.