How to automatically update tag file in vim?
Solution 1
Further to Blixtor's answer, you'll need to think a little carefully about the design of the script. I'd recommend segregating the design such that the autocommand uses the Windows "start" command or similar to run an external script in the background: thereby preventing Vim from being unresponsive while the tag file is generated.
That script could then generate the tag file using a different file name (i.e. not "tags": ctags -R -o newtags .
) and, when ctags is complete, delete tags
and rename newtags
to tags
. This will prevent the tag file from being unavailable in Vim while the generation is done.
Solution 2
I wrote the vim-easytags plug-in to do exactly this. I initialize my tags file once by scanning a whole project (using the command :UpdateTags **/*.[hc]
for example) and afterwards the plug-in will automatically update the tags file as I edit and :update
my source code files in Vim. While it updates the tags file it will block Vim, but because it only scans the current file it doesn't take long.
Update (2014-07-30): Still working on the vim-easytags plug-in :-). Nowadays it supports an asynchronous mode to avoid blocking Vim. At the time I write this the asynchronous mode is not the default mode yet, but after some more feedback I'll probably switch the default mode.
Solution 3
I already wrote a plugin to do all the hard job with ctags: Indexer.
It provides painless automatic tags generation for the whole project(s) and keeps tags up-to-date. Tags is generated in background, so, you do not have to wait while ctags generates tags. You can use it independently or as an add-on for another plugin project.tar.gz.
In the first way, you can declare your projects in ~/.indexer_files
like this:
[CoolProject]
/home/user/cool_project
[AnotherProject]
option:ctags_params = "--languages=c++"
/home/user/another_project/src
/home/user/another_project/lib
And then, when you open any file from /home/user/cool_project
, all this project will be indexed by ctags. When you open tags from another project, tags is generated for it too. Tags from different projects is never mixed. When you save file from project, tags is silently updated. You do not have to care about it, it just works.
For detailed information, see the article: Vim: convenient code navigation for your projects, which explains the usage of Indexer + Vimprj thoroughly.
It is successfully tested on Vim 7.3, on the following systems:
Archlinux
Ubuntu 10.4
Windows XP
Mac OS X Lion
Solution 4
An idea:
Use Vim autocommands (:help autocommand) to trigger running of a script every time a buffer is saved using the BufWritePost event.
This script starts the ctags generation and contains some additional small logic to not run while it's already running (or to run at most every 10 minutes, etc.).
Edit:
Something similar was asked here beforehand, see Vim auto-generate ctags
Solution 5
From its repository: vim-gutentags
is a plugin that takes care of the much needed management of tags files in Vim. It will (re)generate tag files as you work while staying completely out of your way. It will even do its best to keep those tag files out of your way too. It has no dependencies and just works.
You can give it a try at https://github.com/ludovicchabant/vim-gutentags.
Canopus
Updated on May 15, 2020Comments
-
Canopus almost 4 years
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 file. Our code base is huge and it takes quite a while for generating tag file.
Is there any tool which periodically updates the tag file in background? Can I configure VIM to do the same?
I use gvim under Windows.
-
Rafid over 13 years@AI, is there a similar solution for Linux?!
-
StefanTflch almost 13 yearsFor Linux I imagine you could run the command with & to push it to the background, no?
-
Nick Redmark almost 13 yearsactually, the taglist plugin has already the feature of auto-updating its taglist on file changes. the only thing I couldn't find is how that plugin could be integrated with the normal tags, so that ^] uses that list. maybe there's a solution like that (I'd be interested in that). my two cents :)
-
FDinoff over 9 yearsHmmm edit from the future. (While I'm here...) how does this compare to AutoTag.
-
xolox over 9 years@FDinoff: AutoTag seems much simpler, so one differentiation factor is completeness / configurability / complexity. Another difference is that vim-easytags can update and highlight tags while AutoTag seems to focus just on the updating part. Also, AutoTag requires Python while vim-easytags can work without.
-
Tmx over 9 years"This plug-in automatically creates a global tags file and keeps it up-to-date as you edit files in Vim." How can I keep separate tags for different projects instead of single global one? @xolox
-
xolox over 9 years@xterm: Project specific tags files are supported, please check the homepage / readme.
-
osirisgothra about 9 yearsyou might want to use :call system() instead, that way it would be portable, you can get any output via 'let l:lines=system("cmdline")' and the return code is returned in v:shell_error.
-
Hito almost 8 years@xolox Is it possible to execute a first scanning for a project synchronously and then execute async update ? Thanks for the plugin !