How to navigate multiple ctags matches in Vim?
Asked Answered
T

4

71

I'm using Ctrl-] in Vim to navigate using Ctags. How do I navigate to alternate file if there are multiple matches?

Ex. something.publish in a codebase containing multiple occurrences of publish:

class Foo
  def publish
  end
end

class Bar
  def publish
  end
end
Tetrahedral answered 22/1, 2013 at 18:20 Comment(0)
J
89

:tn[ext] goes to the next tag, :tp[revious] goes to the previous one. :ts[elect] gives you a list to choose from.

:help tag-matchlist for more fun and exciting things to try!

Jupon answered 22/1, 2013 at 18:23 Comment(0)
K
127

Use g] instead of C-] to get the list of all matches.

You might want to read :help g]

Keavy answered 22/1, 2013 at 19:25 Comment(2)
How do you select a match from the list of matches displayed by control-] ?Crossfade
If I recall correctly, every match has a number, so you just type that number (e.g. 3) and hit Enter.Keavy
J
89

:tn[ext] goes to the next tag, :tp[revious] goes to the previous one. :ts[elect] gives you a list to choose from.

:help tag-matchlist for more fun and exciting things to try!

Jupon answered 22/1, 2013 at 18:23 Comment(0)
L
34

Adding the answer I was ultimately looking for in case it helps others:

g<C-]> will jump to the tag if there's only one match and will present a list if there are multiple matches.

I've added this mapping to my .vimrc to make it do what I want by default:

nnoremap <C-]> g<C-]>

Leund answered 6/2, 2017 at 22:34 Comment(1)
Very cool.. As, it happens in vim very frequently..I actually accidentally pressed this.. and was wondering how I can get that functionality again :)Greysun
A
1

ltag

ltag the_tag_name
lopen

opens a location window with the tag matches.

This is specially powerful with regular expression tag searches:

ltag /tag_na
lopen

which will list all tags that contain the string tag_na (thus including the_tag_name).

You can then further search inside the location list, before hitting enter to jump to the tag.

To use it for the word under the cursor, you might want to define the map:

nnoremap <leader>l exec("ltag ".expand("<cword>"))<CR>

Then, if you are a tab maniac like me:

command! -nargs=1 Ltag silent ltag <args> | execute "normal \<C-o>" | tab lopen

will open a new tab with the location list and all the matches, instead of jumping to the tab directly:

:Ltag /my_struct
Akiko answered 9/5, 2017 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.