I can use the finddir function to find a parent folder that contains a .git folder (project root), like this:

let dir = finddir('.git/..', ';')

However, this only finds project roots that are parent folders of Vim's current working directory, which is usually the directory Vim was started in. I would like to find project roots that are parent folders of the location of the currently active buffer.

Edit: I tried

let dir = finddir('.git/..', expand('%').';')

But this always returns the current directory for some reason.


1 Answer 1


After following dedowsdi's instructions, it turns out you just need to do:

let dir = finddir('.git/..', expand('%:p:h').';')

expand(%) expands into the path of the current file (excluding the filename).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.