Search code examples
vimtagsctags

No tags file in GVim on some file but not on others


I just installed ctags via homebrew and appended the following line in my ~/.vimrc:

set tags=./tags,tags;$HOME

And then I ran /usr/local/bin/ctags -R . on some of my directories and opened some files stored in the directories, then some of those scripts succeeded in importing tags file but others didn't.

For example, I opened up test.py on my Python workspace, which I already run the above command in, and then I tried to put Ctrl+] on my GVim, it looks like successfully imported the tags file.

I also opened up hello.go located in ~/go/src/github.com/user/hello, in which I already executed the above ctags command, successfully imported the tags file. However, my test.rb file, which I just put on the Go's directory in order to do test purpose, didn't import the tags file correctly.

Also, when I executed the ctags command on ~/another_go_workspace/src, and then opened up the file located in ~/another_go_workspace/src/hello/hello.go, then the file didn't import the tags file... However, since I appended set tags=./tags,tags;$HOME on my ~/.vimrc, doesn't it automatically look for higher directories, right?

So what am I missing?

And if it doesn't import the tags file in higher directories, do I have to execute the ctag command on EVERY directory, i.e. on ~/go/src/soccer_analysis, ~/go/src/coffee, ~/go/src/utility, etc, etc... ?

Thanks.


Solution

  • Your value for the tags option is correct and your assumptions about its behaviour are correct too.

    With your setting, set tags=./tags,tags;$HOME, Vim will search for a tags file in the directory of the current file first then for a tags file from the working directory upward to $HOME.

    This allows you to generate a tags file at the root of your project and be sure that Vim will pick it up wherever you are in your project and whatever the working directory is.

    With the following structure and your current settings:

    project/
        bar/
            bar.js
        foo/
            foo.js
        project.js
        tags
    

    Vim should find tags in all the following scenarios and their variants:

    $ vim project.js
    $ cd foo && vim foo.js
    $ cd bar && vim bar.js
    $ vim foo/foo.js
    $ vim bar/bar.js
    $ cd bar && vim bar.js ../project.js
    

    Every time you add a new file to your project or write to an existing file, you must re-index your whole project. From what you wrote about the ruby file, it looks like you didn't run ctags after adding the file. Try this for a selection of files in your project: :echo tagfiles().