vim-easytags icon indicating copy to clipboard operation
vim-easytags copied to clipboard

Auto-detect project directory

Open marcusbuffett opened this issue 9 years ago • 6 comments

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?

marcusbuffett avatar Apr 22 '15 15:04 marcusbuffett

+1

ike18t avatar May 25 '15 16:05 ike18t

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.

bubifengyun avatar Jun 20 '15 15:06 bubifengyun

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.

hcgraf avatar Nov 30 '15 13:11 hcgraf

@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

QMonkey avatar Jan 19 '16 14:01 QMonkey

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'

jcppkkk avatar Jun 18 '19 07:06 jcppkkk

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

jlmxyz avatar May 24 '21 21:05 jlmxyz