vim-easytags
vim-easytags copied to clipboard
Auto-detect project directory
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?
+1
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.
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.
@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
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'
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