Fix to vims taglist plugin (ctags tags comments in PHP)

Taglist uses exuberant ctags, which supports many languages including PHP. I use vim on a daily basis to edit PHP files. Often classes and functions have PHPDoc and some like to use the words ‘class’ or ‘function’ in their comments. Fair enough, but ctags regex strings ignores that lines may have a preceding ‘*’ or ‘//’ and thus create tags of funny classes and functions. In some cases browsing large files of PHP source code with taglist is pointless.

The solution to this is writing your own ctags configuration file (~/.ctags) and letting ctags know what regex strings You want to tag your PHP files with:


-f ./tags
--langmap=php:+.inc
-h '.php.inc'
-R
--totals=yes
--tag-relative=yes
--PHP-kinds=+cf-v
--regex-PHP
--regex-PHP=/(^|^[^*]+[[:blank:]])class[[:blank:]]+([[:alpha:]][[:alnum:]_]*)/\2/c/
--regex-PHP=/(^|^[^*]+[[:blank:]])function[[:blank:]]+&?[[:blank:]]*([[:alpha:]][[:alnum:]_]*)/\2/f/


6 Responses to Fix to vims taglist plugin (ctags tags comments in PHP)

  1. Upgraded the regex to also ignore double slash commented lines:


    -f ./tags
    --langmap=php:+.inc
    -h '.php.inc'
    -R
    --totals=yes
    --tag-relative=yes
    --PHP-kinds=+cf-v
    --regex-PHP
    --regex-PHP=/(^|^[^\/*]+[[:blank:]])class[[:blank:]]+([[:alpha:]_][[:alnum:]_]*)/\2/c/
    --regex-PHP=/(^|^[^\/*]+[[:blank:]])function[[:blank:]]+&?[[:blank:]]*([[:alpha:]_][[:alnum:]_]*)/\2/f/

  2. Thanks for sharing this, Robert.
    One minor, issue with the updated regex above is the “&”. I’m guessing this was broken by WP when escaping output..etc.
    Also, and I’m not sure if you intentionally left this one out, but to include the magic functions within tags, a small addition to the 2nd regex:
    (^|^[^\/*]+[[:blank:]])function[[:blank:]]+&?[[:blank:]]*(_*[[:alpha:]_][[:alnum:]_]*)

  3. Avatar rojaro
    rojaro says:

    Great stuff.. just what i was looking for 🙂
    Thanks for fixing the regexes

  4. Avatar Bjørn B
    Bjørn B says:

    I was googling for the solution to my tags-problems when I came across this site, and this tip helped solved my problems. Thanks for sharing.

  5. Can u tell me how to do this in windows7 ?

  6. @quqiufeng: $HOME/ctags.cnf is the config file on windows. You should be able to put your configuration there. You’ll also want to check that –version shows +regex, as regex support is required and may not be available on windows. If that’s the case, you might consider trying cygwin or compiling ctags yourself.