xolox / vim-easytags

Automated tag file generation and syntax highlighting of tags in Vim
http://peterodding.com/code/vim/easytags/
1.01k stars 109 forks source link

Auto-detect project directory #119

Open marcusbuffett opened 9 years ago

marcusbuffett commented 9 years ago

It would be nice if there was an option for easytags to look for the nearest ancestor that contains a .git, .svn, .hg, etc. file, and put the .tags in there. Similar to how CtrlP lets you use the 'r' option for the ctrlp_working_path_mode. Any chance this could be added?

ike18t commented 9 years ago

+1

bubifengyun commented 9 years ago

it seems that you can do like this. using ctags ./ in your project root folder. let g:easytags_dynamic_files=1 in your .vimrc script then you can enjoy coding.

hcgraf commented 8 years ago

Using g:easytags_dynamic_files=1 only works if you start vim from your projects root directory, if you start vim from a subdirectory, it will fallback to the global tags file (~/.vimtags).

You can use https://github.com/airblade/vim-rooter to change vim's working directory to the project root automatically when opening a file.

QMonkey commented 8 years ago

@marcusbuffett The code below may solve your problem. But you should install vim-rooter firstly, because the function "FindRootDirectory" comes from vim-rooter.

autocmd BufReadPre,FileReadPre * execute !empty(FindRootDirectory()) ? 'setlocal tags=' . FindRootDirectory() . "/.tags" : 'setlocal tags=./.tags'
let g:easytags_dynamic_files=2
jcppkkk commented 5 years ago

Here is my settings, also works in submodule

let g:easytags_dynamic_files = 2
let gitroot = system("git rev-parse --show-superproject-working-tree --show-toplevel | head -n1 | tr -d '\\n'")
autocmd BufReadPre,FileReadPre * execute !empty(gitroot) ? 'setl tags=' . gitroot . "/.git/vimtags" : 'setl tags=~/.vimtags'
jlmxyz commented 3 years ago

Here is my settings, also works in submodule

let g:easytags_dynamic_files = 2
let gitroot = system("git rev-parse --show-superproject-working-tree --show-toplevel | head -n1 | tr -d '\\n'")
autocmd BufReadPre,FileReadPre * execute !empty(gitroot) ? 'setl tags=' . gitroot . "/.git/vimtags" : 'setl tags=~/.vimtags'

was only working for me if vim started on command line in file directory (eg not vim some/path/to/file) and if there is a git dir in top dirs.... else git display a "fatal : ...." message and return 0

here is a solution :

let g:easytags_dynamic_files = 2
autocmd BufRead,FileReadPre * call FindProjectTag()

function!  FindProjectTag()
    let g:mycurdir =  expand('<afile>:h')
    cd `=g:mycurdir`
    if !exists("g:gitroot")
       let g:gitroot = system("git rev-parse --show-superproject-working-tree --show-toplevel | head -n1 | tr -d '\\n'")
       if -1 != match( expand(g:gitroot), "fatal:")
           let g:gitroot = "fatal"
       endif
    endif
    if expand(g:gitroot) == "fatal" 
        setl tags=~/.vimtags
    else
        let  &l:tags = expand (g:gitroot) . "/tags"
    endif
endfunction