vimtricks.wiki Concise Vim tricks, one at a time.

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 a tags file
  • The ; suffix is the upward-search modifier — without it, Vim only checks that exact path
  • tags (no prefix) — also check a tags file 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:

  1. ~/project/src/lib/tags (not found)
  2. ~/project/src/tags (not found)
  3. ~/project/tags ← found!

Then <C-]> on a function name jumps directly to its definition.

Tips

  • Pair with a Makefile target or git hook that runs ctags -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'

Next

How do I open a file in a read-only split window in Vim?