Skip to content

Instantly share code, notes, and snippets.

@Flolagale
Last active May 25, 2020 00:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Flolagale/5195934 to your computer and use it in GitHub Desktop.
Save Flolagale/5195934 to your computer and use it in GitHub Desktop.
Generate ctags incrementally in vim. Juste paste this into your .vimrc.
function! GenerateTagsIncrementally()
python << EOF
import os
print('Generating tags incrementally.')
# Walk the file tree, if a file has an mtime more recent than the tag file,
# add it to the list of files to index.
tags_mtime = os.stat('tags').st_mtime
with open('list', 'w') as fp:
for dirpath, dirnames, filenames in os.walk(os.getcwd()):
for filename in filenames:
full_path = os.path.join(dirpath, filename)
if os.stat(full_path).st_mtime > tags_mtime:
fp.write(full_path + '\n')
# print(full_path) # Files to be indexed.
# Run ctags using the created list of files.
os.system('ctags --recurse --verbose --append --extra=+q --fields=+aimS --c-kinds=+p --c++-kinds=+p -L list')
os.remove('list')
EOF
endfunction
command GenerateTagsIncrementally call GenerateTagsIncrementally()
@Flolagale
Copy link
Author

Just paste this into your .vimrc, it will provide you with a new command:

:GenerateTagsIncrementally

that will use ctags and update the tags only for the files that have a modification time more recent than the tags file modification time.
This snippet uses vim's current working directory to find the files to index and requires Python, Exuberant Ctags and Vim compiled with Python support.

@taoso
Copy link

taoso commented Mar 2, 2014

I think the magic you use is just the --append option.

You can just use the find command to find all the file modified after the tag file like this:

find . -newer tags #tags is the name of you tag file

However, the option --append does not work well when you delete some code from your source file. Even though a symbol is deleted from the source file, the --append option could not let cags to delete the symbol from the tag file.

@huowa222
Copy link

line 18:
Traceback (most recent call last):
File "", line 5, in no
OSError: [Errno 2] No such file or directory: 'tags'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment