Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim auto-generate ctags

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!

like image 655
cdleary Avatar asked Sep 30 '08 22:09

cdleary


People also ask

How do I set tags in Vim?

You can select a particular tag from the tag list after opening the file in the vim editor by using ctags command. Open any source code in vim editor and type ':tselect' to find out the list of tag list of current source code. Here, the same file, abs_num.py is used to check this command.


1 Answers

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.

like image 178
Zathrus Avatar answered Oct 21 '22 01:10

Zathrus