Vim: Difficulty setting up ctags. Source in subdirectories don't see tags file in project root

add this to .vimrc file set tags=tags;/

This will check the current folder for tags file and keep going one directory up all the way to the root folder.

So you can be in any sub-folder in your project and it'll be able to find the tags files.


There is an option to tell Vim where to look for tag file.

I use the following configuration:

" search first in current directory then file directory for tag file
set tags=tags,./tags

Extract from help :

When a tag file name starts with "./", the '.' is replaced with the path of the current file. This makes it possible to use a tags file in the directory where the current file is (no matter what the current directory is). The idea of using "./" is that you can define which tag file is searched first: In the current directory ("tags,./tags") or in the directory of the current file ("./tags,tags").

For example: :set tags=./tags,tags,/home/user/commontags

And I keep my current working directory in the top project directory where my tagsfile is generated.

Use :pwd and then :cd myproj (inside Vim) to go to the directory containing your tags file.

See :help tags-option for more information on tags path.

You issue is probably that you are either in the wrong directory, or your tags option is not properly set.


#!/bin/sh

FREEZE_NAME=/* Give some version number */

mkdir $HOME/ctags/$FREEZE_NAME

V1=/* Software Path */

find $V1 -name "*.h" | xargs /usr/local/bin/ctags -a -f $HOME/ctags/$FREEZE_NAME/h.tags

find $V1 -name "*.c" | xargs /usr/local/bin/ctags -a -f $HOME/ctags/$FREEZE_NAME/c.tags

cd $HOME/ctags/$FREEZE_NAME/

rm -f all.tags

cat c.tags h.tags >> all.tags

sort all.tags > temp.tags

mv temp.tags all.tags

rm -f c.tags h.tags 

Put the above code in a .sh file and run... This will generate your tags for sure.


If you generate a tags file for every project, you might like this pattern, especially if you share your .vimrc across different machines:

let repohome=substitute($REPO_HOME, "\/", "\\\\/", "g")                         
let &tags=substitute(expand("%:p:h"), "\\(".repohome."/.\\{-}\/\\).*", "\\1tags", "")

You would then have to set the environment variable $REPO_HOME in your .bashrc to your main repo directory without the trailing space (e.g. /home/<yourusername>/repos) and it will automatically look for a tags file in each subdirectory of $REPO_HOME with a depth of 1, e.g. /home/<yourusername>/repos/myproj/tags.