How to automatically update tag file in vim?

后端 未结 7 642
南旧
南旧 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:16

    http://vim.wikia.com/wiki/Autocmd_to_update_ctags_file

    function! DelTagOfFile(file)
      let fullpath = a:file
      let cwd = getcwd()
      let tagfilename = cwd . "/tags"
      let f = substitute(fullpath, cwd . "/", "", "")
      let f = escape(f, './')
      let cmd = 'sed -i "/' . f . '/d" "' . tagfilename . '"'
      let resp = system(cmd)
    endfunction
    
    function! UpdateTags()
      let f = expand("%:p")
      let cwd = getcwd()
      let tagfilename = cwd . "/tags"
      let cmd = 'ctags -a -f ' . tagfilename . ' --c++-kinds=+p --fields=+iaS --extra=+q ' . '"' . f . '"'
      call DelTagOfFile(f)
      let resp = system(cmd)
    endfunction
    autocmd BufWritePost *.cpp,*.h,*.c call UpdateTags()
    
    0 讨论(0)
提交回复
热议问题