Reputation: 1041
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?
Upvotes: 73
Views: 43999
Reputation: 129
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.
Upvotes: 9
Reputation: 1306
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
Upvotes: 114
Reputation: 75740
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
Upvotes: 11
Reputation: 356
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!
Upvotes: 2
Reputation: 10929
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
Upvotes: 5
Reputation: 11847
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
.
Upvotes: 1