How do I jump to a tag in Vim and automatically choose if there is only one match?
Answer
:tjump {name}
Explanation
Vim's :tjump is the smarter sibling of :tag and :tselect. When there is exactly one matching tag it jumps straight there. When multiple definitions exist it shows a numbered list so you can choose. This makes it more useful than the default <C-]> binding (which always takes the first match) and more ergonomic than :tselect (which always shows the list).
How it works
:tjump {name}— jumps directly if there is one match; shows a selection list if there are manyg<C-]>— the normal-mode equivalent: use on the word under the cursor:tselect {name}— always shows the list regardless of the number of matches:tag {name}— always jumps to the first match (no ambiguity resolution)- After jumping, use
<C-t>or:popto go back
Example
With a tag file that has init defined in three places:
:tjump init
# Title File Line Kind Lang
1 init src/app.py 12 f Python
2 init src/db.py 45 f Python
3 init lib/util.py 7 f Python
Type number and <Enter> (empty cancels):
Type the number and press <Enter> to jump to that definition. With a unique tag, the list never appears.
Tips
- Remap
<C-]>tog<C-]>for always-smart behavior:nnoremap <C-]> g<C-]> - Use
:tjump /pattern(with a slash) to search for tags matching a regex rather than an exact name - View the tag stack at any time with
:tagsto see where you jumped from - Works with any ctags-compatible tags file (Universal Ctags, Neovim's
ctag(), LSP via plugins)