Vim auto-generate ctags

cdleary picture cdleary · Oct 1, 2008 · Viewed 32.3k times · Source

Right now I have the following in my .vimrc:

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

There are a few problems with this:

  1. It's slow -- regenerates tags for files that haven't changed since the last tag generation.
  2. I have to push the enter button again after writing the file because of an inevitable "press Enter or type command to continue".

When you combine these two issues I end up pushing the additional enter too soon (before ctags -R has finished), then see the annoying error message, and have to push enter again.

I know it doesn't sound like a big deal, but with the amount of file writes I do on a given day it tends to get really annoying. There's gotta be a better way to do it!

Answer

Zathrus picture Zathrus · Oct 1, 2008

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

The downside is that you won't have a useful tags file until it completes. As long as you're on a *nix system it should be ok to do multiple writes before the previous ctags has completed, but you should test that. On a Windows system it won't put it in the background and it'll complain that the file is locked until the first ctags finishes (which shouldn't cause problems in vim, but you'll end up with a slightly outdated tags file).

Note, you could use the --append option as tonylo suggests, but then you'll have to disable tagbsearch which could mean that tag searches take a lot longer, depending on the size of your tag file.