4

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
|+.../
|~foo/
| |~bar/
| | |-...
| | |-baz.ext # current file
| | `-...
|+.../
AnC
  • 334
  • 4
  • 10
  • Mind telling me how you got that "|" characters in nerdtree? Have been looking for a way to do this but no luck –  May 03 '18 at 10:24
  • I'm afraid I never did, the above was just a manual illustration (if I recall correctly). – AnC May 03 '18 at 13:36
  • I asked in their public repo, the system (feature?!) was disabled **3 years** ago –  May 05 '18 at 06:28

3 Answers3

13

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

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

mraaroncruz
  • 258
  • 2
  • 8
  • though I can't upvote it without a 15 reputation ;) – mraaroncruz Nov 11 '11 at 09:22
  • woohoo! I can upvote. And you have received a present for your kindness. I just had an upvote party! – mraaroncruz 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 08:38
  • Sorry dude. But your upvotes were heard around superuser so something good came out of it (plus I use this answer now in my own .vimrc) – mraaroncruz Nov 18 '11 at 16:52
  • 1
    With the latest version, NERDTreeFind does exactly what I want: https://github.com/scrooloose/nerdtree/pull/72 - so I guess you were prescient, pferdefleisch... – AnC Jan 04 '12 at 14:33
3

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: https://superuser.com/a/474298/175466, 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

Paul Schyska
  • 131
  • 1
0

the "accepted answer" does not resolve the original ask at all. the ask is to not changing the tree root. https://github.com/scrooloose/nerdtree/pull/72 seems the solution but this PR has never been merged into the code. what is the right solution?

Ping
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '22 at 02:56
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://superuser.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://superuser.com/help/whats-reputation), you can also [add a bounty](https://superuser.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/1152574) – Rohit Gupta Oct 22 '22 at 03:34