Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a global vim 'project root' path

Tags:

path

vim

project

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.

like image 950
Adam Hutchinson Avatar asked Nov 22 '11 10:11

Adam Hutchinson


People also ask

How to set the default path in vimrc?

The concept is simple, search the path each time (i.e." set complete+=i ", which is default) but limit what the path is. I decided on a few global scoped variables to hold my paths and then a function/command to help set &path. Seemed easy enough. This is from my vimrc and as you can see it is a mess.

How to change the default directory Vim-Rooter uses to change directory?

By default vim-rooter uses :cd to change directory. To use :lcd or :tcd instead: To stop Rooter echoing the project directory: By default Rooter doesn't resolve symbolic links in the file or directory which triggers it. To do so:

How do I find the location of a specific file in Vim?

Depending on your system, the 'runtimepath' option will be set to several paths, separated by commas. Typing :set runtimepath? will display it. At the end of this list should be some 'after' directories: Typing :help 'rtp' will show documentation that includes a list of every type of file that Vim searches for, as well as the standard locations.

Where can I find vimrc?

for MacVim, it's at /usr/local/Cellar/macvim/7.4-98/MacVim.app/Contents/Resources/vim/vimrc Show activity on this post. Vim appears to have changed to loading a defaults.vim file last in the absence of ~/.vimrc.


1 Answers

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
like image 133
Idan Arye Avatar answered Sep 25 '22 17:09

Idan Arye