7

I know how to refresh NERDTree manually hitting R when focus is in the NERDTree column.

I would like this done automatically.

If an event is needed to trigger this, let it be "whenever writing a file".

Someone suggests adding this to vimrc

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

to map this to a key.

The solution could be a combination of this and autocmd.

Jérôme
  • 263
  • 4
  • 10
  • The answer you're referring to has no upvotes, and in fact doesn't work, because the mapping syntax is wrong; the `\|` needs to be dropped. – Ingo Karkat Nov 03 '16 at 16:54
  • Shouldn't I have posted this on http://vi.stackexchange.com/? Should I ask for migration? (I refrained from posting on SO but didn't go all the way to there...) – Jérôme Nov 03 '16 at 17:09
  • SU has fewer Vim questions, but there are still enough experts here to solve most problems. VI now is most active, followed by SO. Try VI next time, unless it's only marginally related to Vim (like a terminal or install issue). – Ingo Karkat Nov 03 '16 at 17:12
  • @IngoKarkat question updated with quoted vimrc line fixed – Jérôme Nov 03 '16 at 20:14

2 Answers2

3

Putting everything together, something like this would do:

autocmd BufWritePost * NERDTreeFocus | execute 'normal R' | wincmd p

You can add additional autocmd events. However, be aware that above doesn't handle the case when you're currently already in the NERDTree window (but this could be handled with a conditional on &filetype ==# 'nerdtree'). Then, this would also work on events such as CursorHold.

Ingo Karkat
  • 22,638
  • 2
  • 45
  • 58
  • Thanks. So this will refresh only on a write event, right? That is, only when I `:w` in vim, not if an external program modifies the files. Is that it? – Jérôme Nov 03 '16 at 17:00
  • Because then I don't understand the "when you're currently already in the NERDTree window" case. I write files from the file edition window. Not sure I can write files from the NERDTree window. Maybe it is possible but not part of my workflow. – Jérôme Nov 03 '16 at 17:02
  • The autocmd as written should work (didn't test it though). It's just when you _add_ other events (like `CursorHold`, to update when Vim is idle) that it falls apart, and you need to perform additional checks. – Ingo Karkat Nov 03 '16 at 17:09
  • OK, just checked [the docs](http://vimdoc.sourceforge.net/htmldoc/autocmd.html). So your command works only when writing a file (`BufWritePost` event), which I always do from the file edit window, not from NERDTree column, so that's fine. There is no such event to detect a modification of the file tree from another program and `CursorHold` does not really do that. I'm fine with the "only on file write" limitation. I'm used to write files just to let pylint run anyway. – Jérôme Nov 03 '16 at 17:17
  • One downside of this, if I did it correctly, is that it opens NERDTree even if it was closed before the operation. Normally, I don't open NERDTree on vim startup, only manually, depending on my usage. – Jérôme Nov 23 '16 at 14:00
1

Personally I prefer to trigger the refresh automatically when focusing the NERDTree window. This autocommand will do it

autocmd BufEnter NERD_tree_* | execute 'normal R'

Patrick
  • 111
  • 3
  • Yes, sounds better. I added this to my .vimrc and it doesn't seem to work (nothing happens). I added ` | wincmd p` at the end (copied from the other answer) but then I get an error `Error detected while processing BufEnter Autocommands for "NERD_tree_*":` – Jérôme Jan 25 '21 at 08:20
  • The `wincmd p` is not useful here. Anyways, I'm not sure why it doesn't work for you. Any more details to the error message? – Patrick Jan 25 '21 at 14:29