How to automatically update tag file in vim?

后端 未结 7 760
南旧
南旧 2021-01-30 02:32

I use vim C++ tag file for navigation using Ctrl-]. The problem is whenever some file gets modified, the links are no longer valid and I have to re-run ctags and update the tag

7条回答
  •  抹茶落季
    2021-01-30 03:04

    This logic works for most cases: When opening a new file in vim, change to the directory of that file and generate a tags file there if it does not already exist. When saving a changed buffer, generate a tags file in the directory of the file being saved:


    function! GenerateTagsFile()
      if (!filereadable("tags"))
        exec ":!start /min ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --sort=foldcase ."
      endif
    endfunction
    
    " Always change to directory of the buffer currently in focus.
    autocmd! bufenter *.* :cd %:p:h
    autocmd! bufread  *.* :cd %:p:h
    
    " Generate tags on opening an existing file.
    autocmd! bufreadpost *.cpp :call GenerateTagsFile()
    autocmd! bufreadpost *.c   :call GenerateTagsFile()
    autocmd! bufreadpost *.h   :call GenerateTagsFile()
    
    " Generate tags on save. Note that this regenerates tags for all files in current folder.
    autocmd! bufwritepost *.cpp :call GenerateTagsFile()
    autocmd! bufwritepost *.c   :call GenerateTagsFile()
    autocmd! bufwritepost *.h   :call GenerateTagsFile()
    

提交回复
热议问题