1 function! s:CheckAndAddTagFile(path) 2 if stridx(a:path, '/') == (strlen(a:path) - 1) 3 let l:tags = a:path . 'tags' 4 else 5 let l:tags = a:path . '/tags' 6 endif 7 8 if stridx(&tags, l:tags) != -1 9 echo l:tags "already added" 10 return 11 endif 12 13 if !filereadable(l:tags) 14 echo l:tags "not readable" 15 return 16 endif 17 18 let &tags = len(&tags) == 0 ? l:tags : &tags . ',' . l:tags 19 echo l:tags "added" 20 21 unlet! l:tags 22 endfunction 23 24 function! s:StrEndWith(str, pattern) 25 if strridx(a:str, a:pattern) == strlen(a:str) - strlen(a:pattern) 26 return 1 27 else 28 return 0 29 endif 30 endfunction 31 32 function! s:SplitPath(path) 33 let l:start = 0 34 let l:list = [] 35 36 while 1 == 1 37 let l:idx = stridx(a:path, '/', l:start) 38 let l:start = l:idx + 1 39 40 if l:idx == -1 41 break 42 endif 43 44 let l:part = a:path[0:(l:idx > 0 ? l:idx - 1 : l:idx)] 45 call add(l:list, l:part) 46 endwhile 47 48 if !s:StrEndWith(a:path, '/') 49 call add(l:list, a:path) 50 endif 51 52 return l:list 53 endfunction 54 55 function! AddTagsInCwdPath() 56 let l:cwd = tr(expand('%:p:h'), '\', '/') 57 58 let l:pathes = s:SplitPath(l:cwd) 59 60 for p in l:pathes 61 call s:CheckAndAddTagFile(p) 62 endfor 63 64 endfunction 65 66 " call AddTagsInCwdPath()
1. 保存为load_tags.vim, 放到vim/plugin目录下
2. 打开文件之后 :call AddTagsInCwdPath()会将文件所在的目录下以及父目录下的所有tags自动添加到tags配置
3. TODO: 通过autocmd在打开文件时自动调用AddTagsInCwdPath()