How to refresh in NERDTree plugin
Asked Answered
P

6

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?

Persistent answered 21/12, 2011 at 1:47 Comment(3)
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.Wheal
@JamesMcLaughlin Add that as an answer. Will upvote.Cingulum
possible duplicate of NERDTree reload new filesSkyeskyhigh
F
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

Flats answered 5/10, 2013 at 11:41 Comment(0)
D
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

Dhahran answered 22/10, 2016 at 21:39 Comment(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.Dhahran
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>).Pyrimidine
E
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.

Eberhard answered 15/1, 2021 at 20:47 Comment(4)
This should be the accepted answer. Works perfectlyIrmgardirmina
Great, thanks for sharing! I'm now using this - with the addition that I use :NERDTreeFind if there is an active buffer.Tetrapody
I expect this to make NERDTree hang for minutes if root has a lot of childs (like / or such)Spatz
You may be right. That was not a use case I had to concern myself with since I only use VIM in project folders.Eberhard
T
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

Truncate answered 22/12, 2011 at 7:7 Comment(0)
T
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!

To answered 16/9, 2016 at 6:58 Comment(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.Circinate
M
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.

Marianelamariani answered 21/12, 2011 at 1:52 Comment(1)
It can be automated with this setting: let NERDTreeChDirMode=2Clop

© 2022 - 2024 — McMap. All rights reserved.