How to refresh taglist in vim?

前端 未结 4 714
攒了一身酷
攒了一身酷 2021-02-14 13:27

When I make a change to a file, for example, add a function, how can I make the taglist automatically update the \"tag list\" in its windows after I save the change?

相关标签:
4条回答
  • 2021-02-14 13:53

    I did write a little experimental script that automatically and incrementally updates, the "current" tags file on file saving.

    (The question is actually redundant with Vim auto-generate ctags )

    0 讨论(0)
  • 2021-02-14 13:54

    Haven't tested, but you could try something like:

    au BufWritePre     *.cpp ks|!ctags %
    

    Which basically executes ctags when the buffer for a file ending in .cpp gets saved(:w).

    0 讨论(0)
  • 2021-02-14 13:56

    I adapted my setup from the C++ code completion vim tip.

    map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
    

    When needed, I press Ctrl-F12 to regenerate tags.

    If you're using vim-taglist, you could add to your .vimrc an autocommand for the BufWritePost event to update the taglist window after every save:

    autocmd BufWritePost *.cpp :TlistUpdate
    
    0 讨论(0)
  • 2021-02-14 14:01

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

    Just add this to your ~/.vimrc

    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)
提交回复
热议问题