Search code examples
c++vimctags

vim and ctags, multiple definitions with same signature not found


I have a simple c++ file like this:

class SomeClass
{
    void SomeMethod() {};
};

class AnotherClass
{
    void SomeMethod() {};
};

If I generate a ctags file with ctags -R * I got:

AnotherClass    main.cpp    /^    class AnotherClass$/;"    c   file:
SomeClass   main.cpp    /^    class SomeClass$/;"   c   file:
SomeMethod  main.cpp    /^        void SomeMethod() {};$/;" f   class:AnotherClass  typeref:typename:void   file:
SomeMethod  main.cpp    /^        void SomeMethod() {};$/;" f   class:SomeClass typeref:typename:void   file:

As you can see, there is no difference in the entries for the definition of SomeMethod and as this in vim tf and tn shows always first definition in file.

I also can add a line number with ctags --fields=+n * which results in:

...
AnotherClass    main.cpp    /^    class AnotherClass$/;"    c   line:6  file:
SomeClass   main.cpp    /^    class SomeClass$/;"   c   line:1  file:
SomeMethod  main.cpp    /^        void SomeMethod() {};$/;" f   line:3  class:SomeClass typeref:typename:void   file:
SomeMethod  main.cpp    /^        void SomeMethod() {};$/;" f   line:8  class:AnotherClass  typeref:typename:void   file:

But vim also jumps always to the first definition of SomeMethod if I use ta SomeMethod and tn

I am using ctags in this version:

Universal Ctags 0.0.0(2614dbe1), Copyright (C) 2015 Universal Ctags Team Universal Ctags is derived from Exuberant Ctags. Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert Compiled: Sep 5 2019, 13:10:38 URL: https://ctags.io/ Optional compiled features: +wildcards, +regex, +iconv, +option-directory, +xpath, +yaml, +packcc

and vim is:

VIM - Vi IMproved 8.1 Patch 1-1713


Solution

  • I found a simple solution myself:

    ctags --excmd=number *
    

    did the job.

    In the third column of the tags file we find the excmd which vim uses to find the place in the file.

    SomeMethod  main.cpp    /^        void SomeMethod() {};$/;" f   line:8  class:AnotherClass  typeref:typename:void   file:
    

    becomes

    SomeMethod  main.cpp    8;" f   line:8  class:AnotherClass  typeref:typename    :void   file:
    

    Now vim did not search for the expression which is duplicated in the tags file but simply goes to the line which is in the excmd.