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

How do I navigate through code using tags and the tag stack?

Answer

<C-]> / <C-t>

Explanation

The <C-]> command jumps to the definition of the keyword under the cursor using a tags file, and <C-t> jumps back. Vim maintains a tag stack that works like a browser history, letting you dive deep into code and unwind back.

Setup

Generate a tags file with Universal Ctags:

ctags -R --languages=python,go,javascript .

This creates a tags file in your project root that Vim reads automatically.

Navigation

<C-]>       " Jump to definition of word under cursor
<C-t>       " Pop back to where you jumped from
:tag name   " Jump to the definition of 'name'
:tselect    " If multiple matches, show a selection list
g<C-]>      " Like <C-]> but show selection if multiple matches
:tags       " Show the tag stack

Example workflow

  1. Cursor is on processRequest in main.go
  2. <C-]> — jumps to the definition in handler.go
  3. Cursor is on validateInput inside that function
  4. <C-]> — jumps to its definition in validator.go
  5. <C-t> — back to handler.go
  6. <C-t> — back to main.go

Tips

  • :tnext and :tprev cycle through multiple tag matches
  • Use :stag name to open the tag in a new split
  • Set :set tags=./tags; to search upward for the tags file from the current file's directory
  • In Neovim with LSP, gd and <C-]> can use the LSP for more accurate navigation
  • The tag stack is separate from the jumplist — <C-t> only pops tags, while <C-o> works with all jumps

Next

How do I run the same command across all windows, buffers, or tabs?