sponsor Vim development Vim logo Vim Book Ad

AutoTag : Updates entries in a tags file automatically when saving

 script karma  Rating 407/145, Downloaded by 11172  Comments, bugs, improvements  Vim wiki

created by
Craig Emery
script type
If you use ctags to make tags files of your source, it's nice to be able to re-run ctags on a source file when you save it.

However, using ctags -a will only change existing entries in a tags file or add new ones. It doesn't delete entries that no longer exist. Should you delete an entity from your source file that's represented by an entry in a tags file, that entry will remain after calling ctags -a.

This python function will do two things:

1) It will search for a tags file starting in the directory where your source file resides and moving up a directory at a time until it either finds one or runs out of directories to try.

2) Should it find a tags file, it will then delete all entries in said tags file referencing the source file you've just saved and then execute ctags -a on that source file using the relative path to the source file from the tags file.

This way, every time you save a file, your tags file will be seamlessly updated.

The master copy of this is in my github files:
install details
Simply source the file autoTag.vim from your .vimrc file.
This utility will (obviously) only work when using vim that's been compiled with python support.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
autotag.vim 1.13 2012-02-09 7.0 Craig Emery Use a defaultdict for easier appending.
Pass around dir for found tags file for better integration with fugitive (thanks to Markus Bertheau).
autotag.vim 1.12.2 2012-02-06 7.0 Craig Emery Fixed vim_global(), you can now change the verbosity of the debugging on the fly.

:call AutoTagDebug()

to get the debug window and if you lower the verbosity threshold below 30 (logging.WARNING) you'll start to see debug lines about progress. At logging.WARNING the code only emits warnings about failures.
autotag.vim 1.12.1 2012-02-01 7.0 Craig Emery Fixed the Disabled flag handling
autotag.vim 1.12 2012-01-31 7.0 Craig Emery Pythonic debugging (do :call AutoTagDebug() to see debugging)
Better management of default settings
Ability to stop autotag searching for a tags file if is climbs the tree and reaches a certain directory (likely $HOME, say)
Configurable max tag file size
autotag.vim 1.11 2011-02-23 7.0 Craig Emery Avoid using fileinput.input() as per Tim Cuthbertson's patch. Thanks!
autotag.vim 1.10 2010-02-23 6.0 Craig Emery Minor bug-fix from Manpreet.
autotag.vim 1.9 2009-07-30 6.0 Craig Emery Minor change to support updating the taglist menu (for script #273)
Thanks Nick.
autotag.vim 1.8 2009-06-30 6.0 Craig Emery Tidied up the diag stuff, using *much* more meaningful variables.
Fixed the empty default tags file name.
Confirmed working in MacVim, other platforms not verified. :-$
autotag.vim 1.7 2009-01-29 7.0 Craig Emery More backwards compatibility aimed at Python 2.3.
autotag.vim 1.6 2009-01-29 6.0 Craig Emery Support Python older than 2.4.
autotag.vim 1.5 2008-12-09 6.0 Craig Emery Improved support for Python 2.6 (with some help from Andreas Schneider: cheers!) by moving to Python 2.4 subprocess API.
autotag.vim 1.4 2007-06-18 6.0 Craig Emery Changed the way the script prints messages.
Now there's a run-time configurable verbosity level in the script.
By default it's 0 (zero) so no messages are printed.
I use level 1 (one) and get a nice quick message telling me which ctags file is being updated.
autotag.vim 1.3 2007-06-13 6.0 Craig Emery Submission from Johan Segolsson added configuration of the ctags command name.
Thanks Johan.
autotag.vim 1.2 2007-06-11 6.0 Craig Emery 1.1 would re-declare the Python class on every write.
I've also made the information messages better.
If you experience a pause during the update, the messages should now let you know where it's happening. :-)
autotag.vim 1.1 2005-10-11 6.0 Craig Emery Fixed a couple of small issues:
Files with spaces in their names / paths are now quoted before being given as arguments to ctags
(though I plan to change this code from using os.popen to using os.execl so as to completely avoid these kinds of issues)
If the tags file is found in \ (e.g. c:\) then I was making the relative path incorrectly. This is fixed.
I've cleaned up the handling of '/' and '\' in paths (since tags files specify files with '/' but your OS may not)
autotag.vim 1.0 2005-09-02 6.0 Craig Emery Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.