Is it possible to add specific tag files to particular windows/buffers in vim?

Viewed 165

Is is possible to detect keywords in the path of the current file open in vim and add a particular tag file?

My project is mainly organized in src, test and external blocks, but each of these blocks appear anywhere in the path, not necessarily at the beginning or at the end (otherwise this solution could work). That is, my project has this structure:

feature1/src
feature1/test
feature2/src
feature2/test
external/feature3

I can generate a separate tag file for each src, test and external block and then detect these three keywords in the path in vim with match(expand('%:p:h'), ...).

But how can I only load src tag if I am at any path containing src, and so forth for test and external? Command set tags+=... adds them for all windows (buffers) and mixing them causes many name collisions.

Thanks in advance

2 Answers

Kind of

augroup example | au!
    autocmd BufRead,BufNewFile */myproject/*
        \ let &l:tags = &g:tags . ',' . TagForFile(expand("<amatch>"))
augroup end

Where writing the function TagForFile() is upon you.

Vim already handles that scenario by way of a properly set &tags option:

set tags=./tags;

The value above tells Vim to look for a tags file:

  • in the directory of the current file, with ./tags,
  • recursively, up to the root of your file system, with ; (see :help 'tags' for limiting).

In this case…

  • with the value above and while editing feature1/src/dir/file.c, Vim is going to look for a tags file at the following locations, in order:

    feature1/src/dir/tags
    feature1/src/tags
    feature1/tags
    ...
    
  • with the value above and while editing external/feature3/file.c, Vim is going to look for a tags file at the following locations, in order:

    external/feature3/tags
    external/tags
    ...
    
Related