Vim auto-generate ctags

前端 未结 12 1848
醉话见心
醉话见心 2020-11-29 16:18

Right now I have the following in my .vimrc:

au BufWritePost *.c,*.cpp,*.h !ctags -R

There are a few problems with this:

相关标签:
12条回答
  • 2020-11-29 16:51

    In my opninion, plugin Indexer is better.

    http://www.vim.org/scripts/script.php?script_id=3221

    It can be:

    1) an add-on for project.tar.gz

    2) an independent plugin

    • background tags generation (you have not wait while ctags works)
    • multiple projects supported
    0 讨论(0)
  • 2020-11-29 16:51

    There is a vim plugin called AutoTag for this that works really well.

    If you have taglist installed it will also update that for you.

    0 讨论(0)
  • 2020-11-29 16:52

    I wrote easytags.vim to do just this: automatically update and highlight tags. The plug-in can be configured to update just the file being edited or all files in the directory of the file being edited (recursively). It can use a global tags file, file type specific tags files and project specific tags files.

    0 讨论(0)
  • 2020-11-29 16:55

    Edit: A solution very much along the lines of the following has been posted as the AutoTag vim script. Note that the script needs a vim with Python support, however.

    My solution shells out to awk instead, so it should work on many more systems.


    au FileType {c,cpp} au BufWritePost <buffer> silent ! [ -e tags ] &&
        \ ( awk -F'\t' '$2\!="%:gs/'/'\''/"{print}' tags ; ctags -f- '%:gs/'/'\''/' )
        \ | sort -t$'\t' -k1,1 -o tags.new && mv tags.new tags
    

    Note that you can only write it this way in a script, otherwise it has to go on a single line.

    There’s lot going on in there:

    1. This auto-command triggers when a file has been detected to be C or C++, and adds in turn a buffer-local auto-command that is triggered by the BufWritePost event.

    2. It uses the % placeholder which is replaced by the buffer’s filename at execution time, together with the :gs modifier used to shell-quote the filename (by turning any embedded single-quotes into quote-escape-quote-quote).

    3. That way it runs a shell command that checks if a tags file exists, in which case its content is printed except for the lines that refer to the just-saved file, meanwhile ctags is invoked on just the just-saved file, and the result is then sorted and put back into place.

    Caveat implementor: this assumes everything is in the same directory and that that is also the buffer-local current directory. I have not given any thought to path mangling.

    0 讨论(0)
  • 2020-11-29 16:55

    How about having ctags scheduled to run via crontab? If your project tree is fairly stable in it's structure, that should be doable?

    0 讨论(0)
  • 2020-11-29 17:03

    To suppress the "press enter" prompt, use :silent.

    0 讨论(0)
提交回复
热议问题