Vim and Ctags: Ignoring certain files while generating tags
Asked Answered
T

4

30

I have a folder llvm2.9 in which i ran this command.

$> ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++

This was indexing methods in *.html files also which were present in llvm2.9/docs. I found this out because when i pressed ctrl-] for some class, it went to the html file.

How do i force ctags to use .cpp/.h files alone or ignore a particular directory.

Thanks

Trotyl answered 12/10, 2011 at 7:33 Comment(4)
it would seem that 80% of the problem is caused by forcing the language to cpp. Why do you do that?Sexed
Otherwise, clicking on certain function definition takes me to a html file where it is listed instead of a cpp file.Trotyl
Are you sure? my guess is that processing html files without --language-force=C++ will not pick up the C++ definitions inside the HTML in the first place. Have you verified that? Also, I guess you've seen my answer (I understood the question, I just wanted to challenge the premise a little too)Sexed
related #8193678Champac
I
29

You can exclude a filetype using --exclude='*.html'

Illuminance answered 12/10, 2011 at 15:0 Comment(2)
(this works for exeburant ctags, not sure about the regular tags program)Illuminance
and what about multiple types? Seperated by comma?Pilotage
H
18

If you need to exclude more than just .html files:

You can't comma separate a list inside an exclude option. This doesn't work:

ctags --exclude=*.html,*.js ./*

However, you can pass multiple exclude options:

ctags --exclude=*.html --exclude=*.js ./*

Pass the -V option to help with debugging:

ctags -V --exclude=*.html --exclude=*.js ./*

Gives the output:

Reading initial options from command line
  Option: --exclude=*.html
    adding exclude pattern: *.html
  Option: --exclude=*.js
    adding exclude pattern: *.js
Hammy answered 17/9, 2013 at 18:39 Comment(1)
Quote your exclude patterns to avoid shell expansion.Foyer
S
6

The simplest way in vim would be

 :!ctags {.,**}/*.{cpp,h}

Explanation: The braces expand to

:!ctags ./*.cpp **/*.cpp **/*.h **/*.h 

So it looks for source or header files in the current directory (./) or any nested directory (**/). Note **/ wouldn't match the current directory (it always matches at least 1 sub directory level)

In shell:

 find -iname '*.cpp' -o '*.h' -print0 | xargs -0 ctags

Explanation: This recursively finds all .cpp and .h files under the current directory and passes them to ctags on the command line.

The way print0 and -0 work together is to ensure it works correctly with weird filenames (e.g. containing whitespace or even new line characters)

I'll leave the rest of the ctags options for your own imagination :)

PS. For recent bash-es, you can use

 shopt -s globstar
 ctags {.,**}/*.{cpp,h}

and get much the same behaviour as in vim !

Sexed answered 12/10, 2011 at 7:38 Comment(1)
Can you explain what is happening with each of these commands?Loree
N
2

I didn't want to track down every filetype which might get processed in a large project, and I was only interested in Python, so I explicitly only processed python files using ctags --languages=Python .... The list of language names can be seen using ctags --list-languages.

Nicolas answered 25/8, 2016 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.