Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to make NERDTree expand to display the current file's directory - without messing with the working directory or changing the tree root?

So when working with baz.ext, it should look like this without manually having to expand the respective trees:

/ # project root
| |~bar/
| | |-...
| | |-baz.ext # current file
| | `-...
share|improve this question
up vote 8 down vote accepted

Vim: How to synchronize NERDTree with current opened tab file path?

the map <leader>r :NERDTreeFind<cr> answer works for me.

share|improve this answer
though I can't upvote it without a 15 reputation ;) – pferdefleisch Nov 11 '11 at 9:22
Wow, how could I miss that - thanks a lot! – AnC Nov 11 '11 at 11:02
woohoo! I can upvote. And you have received a present for your kindness. I just had an upvote party! – pferdefleisch Nov 11 '11 at 12:28
Actually, turns out this wasn't quite what I'd hoped for: The tree does not expand, but rather uses the current file's directory as root. (So in the example above, I'd just see the bar directory.) Since there doesn't seem to be an easy solution to this, the above's still good enough though. – AnC Nov 14 '11 at 8:38
With the latest version, NERDTreeFind does exactly what I want: - so I guess you were prescient, pferdefleisch... – AnC Jan 4 '12 at 14:33

I was looking for a solution for doing this automatically on every BufEnter. The naive approach didn't work as I couldn't leave my NERDTree anymore :-) - NERDTreeFind changes the window to the NERDTree and when you go back to the window with the file, it triggers again.

I found my solution in:, but I had a slight problem with it: I normally set my cwd to the project root and then NERDTree and CtrlP around. Occasionally I want to edit a file outside the cwd, like my ~/.vimrc, and NERDTreeFind would change my cwd to my home folder. This also changes the tree root and when I switch back, NERDTreeFind would search the file from my home directory downwards (It's huge).

So I extended @Lambart's solution with a function that checks if the current file is inside the cwd and only NERDTreeFinds if it is.

You can find it in this gist

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .