Making Unite.vim use the project root directory

I’ve been using vim for quite a while now - I think it’s about 3 years . Over the short 3 year journey, I’ve built up a pretty massive vimrc file which I add & tweak almost every day. Over the past week I decided to make the switch over to Unite.vim, but coming from ctrlp, I wasn’t satisfied with the default behaviour.


There are plenty of ctrlp snippets out there which approximate ctrlp’s behaviour, but one thing they don’t focus on is ctrlp’s ability to find your project root and search within its context. For me, this functionality was what took ctrlp from an amazing vim plugin, to something that made massive improvements to the speed of navigation, and ultimately, my productivity.


So I set about finding out how to make unite use the project root when doing it’s ctrlp-like fuzzy search.

I came across a vim project called vim-projectroot. vim-projectroot allows you to retrieve the root path of a project. It does this by detecting common files/directories within a project root, such as .git directories.

So it became clear the solution was to stick them together!

" Build the ctrlp function, using projectroot to define the
" working directory.
function! Unite_ctrlp()
  execute ':Unite  -buffer-name=files -start-insert buffer file_rec/async:'.ProjectRootGuess().'/'

nnoremap <C-P> :call Unite_ctrlp()<cr>

This method executes Unite.vim with the common “ctrlp” settings, but ensuring we use the Project Root.