How do I configure Vim to find a tags file by searching up through parent directories automatically?
Answer
:set tags+=./tags;,tags
Explanation
By default, Vim only looks for a tags file in the current directory. For projects where ctags generates a single tags file at the project root, this means :tag and <C-]> often fail to find definitions when editing files in subdirectories. The solution is to add ./tags; to the 'tags' option — the semicolon tells Vim to search upward through parent directories until it finds a tags file.
How it works
./tags;— start from the directory of the current file, then search parent directories upward until reaching the filesystem root or finding atagsfile- The
;suffix is the upward-search modifier — without it, Vim only checks that exact path tags(no prefix) — also check atagsfile in the current working directory- The comma separates multiple search paths
Example
Add to your vimrc:
set tags+=./tags;,tags
With this structure:
~/project/
tags ← ctags generated here
src/
lib/
parser.go ← editing this file
When editing parser.go, Vim now searches:
~/project/src/lib/tags(not found)~/project/src/tags(not found)~/project/tags← found!
Then <C-]> on a function name jumps directly to its definition.
Tips
- Pair with a
Makefiletarget or git hook that runsctags -R .at the project root - Use
:set tags?to inspect the current value - For very large codebases, consider
set tags=./tags;without the fallback to avoid slow searches - The
;upward-search modifier works for any option that accepts file paths, such as'path'