Adam Hutchinson
Adam Hutchinson

Reputation: 264

How to set a global vim 'project root' path

When I'm working in vim, I usually have multiple buffers on screen at once and need to switch between them and open new files a lot. I've installed the Command-T plugin to facilitate fast file switching but I'm having a few issues with it.

Whenever I press Command-T, it only shows files in the same folder (and sub folders) of the file I am currently editing.

I want it to show me all of the files in my project i.e.:

Project Folder:
~/project1/

Current Buffer:
~/project1/front/js/file1.js

When I press Command+T in the buffer, I want it to display files starting from the project folder.

Thanks.

Upvotes: 7

Views: 7325

Answers (5)

David Brown
David Brown

Reputation: 13526

The problem you have is because CommandT searches in present working directory. You can see what your working directory is with the :pwd command. When you open a file with the gui it sets your present working directory to the directory that file contains. As romainl said you can simply :cd into your project's root directory and then CommandT will search from there (remember to :CommandTFlush)

Alternatively you could use ctrl-p (which I personally prefer since it doesn't require compiling a C helper program). It has an option ctrlp_working_path_mode option changes the working directory to the first ancestor that contains one of the following directories or files:

.git/
.hg/
.bzr/
_darcs/
root.dir
.vimprojects

Upvotes: 3

Idan Arye
Idan Arye

Reputation: 12603

Put the following function in your .vimrc:

function! FindProjectRoot(lookFor)
    let pathMaker='%:p'
    while(len(expand(pathMaker))>1)
        let pathMaker=pathMaker.':h'
        let fileToCheck=expand(pathMaker).'/'.a:lookFor
        if filereadable(fileToCheck)||isdirectory(fileToCheck)
            return expand(pathMaker)
        endif
    endwhile
    return 0
endfunction

That function will search upward from to current file for a parent folder that contains a file or folder with the specified name, and returns that path. That way, you can put a project file in the project's root folder, and send FindProjectRoot('project') as an argument to Command-T

Or even better - call that file project.vim, and use it to load specific settings and keybindings for each project.

EDIT that function will only work on linux. Use this function instead:

function! FindProjectRoot(lookFor)
    let pathMaker='%:p'
    while(len(expand(pathMaker))>len(expand(pathMaker.':h')))
        let pathMaker=pathMaker.':h'
        let fileToCheck=expand(pathMaker).'/'.a:lookFor
        if filereadable(fileToCheck)||isdirectory(fileToCheck)
            return expand(pathMaker)
        endif
    endwhile
    return 0
endfunction

Upvotes: 4

lucapette
lucapette

Reputation: 20724

Disclaimer: I wrote the following plugin

I would suggest to use codepath.vim if you are used to store all your code in the same dir. So, you could do the following:

map <silent> <C-T> :CommandT <c-r>=codepath#path()<CR><CR>
imap <silent> <C-T> <ESC>:CommandT <c-r>=codepath#path()<CR><CR>

And CommandT will open itself in the root dir of your project.

Upvotes: 2

romainl
romainl

Reputation: 196566

:help command-t-usage recommends to :cd into your project folder before using it.

You can also pass it a path as argument as in

:CommandT ..

Upvotes: 3

Prince Goulash
Prince Goulash

Reputation: 15715

I don't know about the Command-T plugin, but you could try set path=<project_path> to set Vim's search path for files.

If not, setting :cd <path> would probably work, but that's rather a drastic solution!

Upvotes: 0

Related Questions