Wednesday, May 24, 2006

making vim and ctags real friends

When one has multiple working copies of the source code repository, managing tags can be quite a burden.

The following script from my .vimrc does the trick - it looks for tags file in the enclosing directories and adds first match to the vim's 'tags' variable. The script is also modified to take care of Win32 version of vim.



let tag_dir=getcwd()
if match(tag_dir, "^/") == 0
let end_dir='/'
while !filereadable(tag_dir."/tags") && tag_dir!=$HOME && tag_dir!=end_dir
let tag_dir = substitute(tag_dir, '/[^/]\+$', "", "")
endwhile
if filereadable(tag_dir."/tags")
exe "set tags+=".tag_dir."/tags"
endif
elseif match(tag_dir, "^[a-zA-Z]:[\\/]") == 0
let end_dir=tag_dir[0].tag_dir[1]
while !filereadable(tag_dir."\\tags") && tag_dir!=end_dir
let tag_dir = substitute(tag_dir, '\\[^\\]\+$', "", "")
endwhile
if filereadable(tag_dir."\\tags")
exe "set tags+=".tag_dir."\\tags"
endif
endif



Lookup stops when root or home directory is reached.

First script checks to see if the system Unix or Win32: '/' as first character of pwd identifies Unix system (or CygWin). Then script check to see if there is "tags" file in the current working directory. If not found it strips from path one directory level and tries in loop again. Loop ends when "tags" file found or root/$HOME directory reached. If "tags" file was found script adds it to 'tags' vim variable.

Update: you can find newer version of my script here.

3 comments:

sparkleyden said...

your vimrc ctags script is awesome! Thanks!

Paul Mikesell said...

That's really great. Thanks for posting that.

Anonymous said...

superb script.
just one issue, its creating problems if directory path includes a symbolic link.