Vim Tips Wiki
Register
Advertisement
Tip 858 Printable Monobook Previous Next

created 2005 · complexity basic · version 6.0


This vimrc code will setup your tags variable, according to the source tree you are working in. This is useful when you have multiple source trees with duplicate tags in each branch.

If you work in a single source tree, then you can hardcode

:set tags=$SRC/tags

and ignore this tip.

Example:

cd /a ; ctags -r . # creates /a/tags
cd /k ; ctags -r . # creates /k/tags
cd /x ; ctags -r . # creates /x/tags

cd /x/y/z ; vim /a/b/c/d/e/f/h/i.c
:set tags
  tags=/a/tags,/x/y/tags

cd /k/l/m ; vim i.c
:set tags
  tags=/k/tags .. vim found the tags in parent of PWD and parent

Put this code in your vimrc:

" Your customised tags go first.
set tags+=~/tags,$DOC/tags
let parent_dir = expand("%:p:h")."/"
while match(parent_dir,"/",0)>-1 && isdirectory(parent_dir)
  let parent_tag = parent_dir."tags"
  if filereadable(parent_tag)
    exe ":set tags+=".parent_tag
  endif
  let parent_dir = substitute(parent_dir,"[^/]*/$","","")
endwhile
let parent_dir = getcwd()."/"
while match(parent_dir,"/",0)>-1 && isdirectory(parent_dir)
  let parent_tag = parent_dir."tags"
  if filereadable(parent_tag)
    exe ":set tags+=".parent_tag
  endif
  let parent_dir = substitute(parent_dir,"[^/]*/$","","")
endwhile
unlet parent_dir parent_tag

Comments[]

I use vim-session files to manage this and many others capabilities of Vim. You see, using vimsession for each project you work on, you may specify (and save) path of tags-files and any other options specific for each project. Extra-session-file lets you create additional functions, procedures, syntax (and other things not allowed in vimsession), specific for the session.

I think, in most cases this way is much easier and useful.


Advertisement