0

I'm looking to write foos,bars,bazs by using a custom language in ctags. It works fine, I can jump to the tag but I'm only allowed to have one unique tag name. I would like to be able to jump through all the tags in different files with a tag named todo1. Ctags is not generating more than one of the same tag name. If I fix this then the next question would be how to jump in vim to the same tag names? I presume :tn probably would not work because I've tried having the same tag names in asciidoc in the past.

For example:

[todo1]
This is something important in some/file/path/foo.txt
[todo1]
This is something important in another/path/bar.txt 
[foo]
Some other foo.

My ctags config file is

--langdef=text
--langmap=text:.txt
--regex-text=/^\[([A-Za-z0-9]+)\]/\1/s,section/

In vim I do.

:! ctags -f ~/text/.tags -R ~/text/
:setlocal tags=~/text/.tags

ctags --version

Universal Ctags 5.9.0(b49410f), Copyright (C) 2015 Universal Ctags Team
Universal Ctags is derived from Exuberant Ctags.
Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Dec 16 2020, 11:27:14
  URL: https://ctags.io/
  Optional compiled features: +wildcards, +regex, +iconv, +option-directory, +xpath, +packcc
2
  • 1
    Welcome to Vi and Vim! You might want to check out :help :tselect
    – D. Ben Knoble
    Jan 16 at 21:58
  • @D.BenKnoble Thanks. How would I go about generating a tags file that contains the same tags names? I currently have an asciidoc file with the same tags name and it wont jump to any of them even with tn or tselect. I can see in the tags that they are there though cause it shows subsection:Filter [#lambda] I have another tag the main heading named lambda also.
    – ritchie
    Jan 16 at 23:19
1

The answer is very simple yet many people don't know this so I'm going to post the answer so that it can help someone out in the future.

I'm not sure if ctags allows for same name tags but it's irrelevant. What you can do is generate your own tags as explained in :help tags-file-format. This can be accomplished with a script containing less than 20 lines of code with a program like rip grep or you can use vimwiki plugin which implements this feature.

Here is an example of a simple tags file that jumps to bar:

bar exampleFile.txt 9
bar exampleFile.txt 17
1
  • 1
    Did you write code? It might be worth including that, too
    – D. Ben Knoble
    Jan 18 at 1:20
0

I'm looking to write foos,bars,bazs by using a custom language in ctags. It works fine, I can jump to the tag but I'm only allowed to have one unique tag name. I would like to be able to jump through all the tags in different files with a tag named todo1. Ctags is not generating more than one of the same tag name.

This is wrong. ctags can generate tags for each name even if two of them have the same name.

If you are using Universal Ctags, use --excmd=combine option.

$ cat /tmp/foo.txt
[todo1]
This is something important in some/file/path/foo.txt

[todo1]
This is something important in another/path/bar.txt 

[foo]
Some other foo.

$ u-ctags -o - --langdef=text --langmap=text:.txt '--regex-text=/^\[([A-Za-z0-9]+)\]/\1/s,section/' /tmp/foo.txt 
foo /tmp/foo.txt    /^[foo]$/;" s
todo1   /tmp/foo.txt    /^[todo1]$/;"   s
$ u-ctags -o - --excmd=combine --langdef=text --langmap=text:.txt '--regex-text=/^\[([A-Za-z0-9]+)\]/\1/s,section/' /tmp/foo.txt 
foo /tmp/foo.txt    7;/^[foo]$/;"   s
todo1   /tmp/foo.txt    1;/^[todo1]$/;" s
todo1   /tmp/foo.txt    4;/^[todo1]$/;" s
5
  • I'm trying your example, and unfortunately (because of the Ex commands used) vim cannot distinguish between the two tags; it always goes to the first one. It uses the search and ignores the line numbers.
    – D. Ben Knoble
    Mar 9 at 16:49
  • Could you try --excmd=combine instead of --fields=+n ? Mar 10 at 2:58
  • Looks like it will work, I’ll give it a try later
    – D. Ben Knoble
    Mar 10 at 13:19
  • This was already resolved by @masatake --> Add --fields=+n option (or --fields=+'{line}' option in Universal Ctags) to the ctags command line. That adds a line: field to each tag entry. The line: fields may make the tag entries unique.
    – ritchie
    Mar 11 at 5:58
  • I think my issue is that my output doesn't match yours; I get a different format which does correctly work.
    – D. Ben Knoble
    Mar 13 at 4:12
0

Thanks to @masatake. This has already been resolved.

Add --fields=+n option (or --fields=+'{line}' option in Universal Ctags) to the ctags command line. That adds a line: field to each tag entry. The line: fields may make the tag entries unique.

4
  • @D.BenKnoble It's been a while since I messed with this so only take what I'm going to say with a grain of salt but maybe it will give you other ideas. There is directory called ~/.ctags.d and in there I have a file text.ctags with this. Maybe it will give you some ideas. 1 │ --langdef=text 2 │ --langmap=text:.txt 3 │ --regex-text=/^[([A-Za-z0-9]+)]/\1/s,section/ . Oh ya make sure you have the correct ctags from github.com/universal-ctags/homebrew-universal-ctags . Not sure what OS you're using but this is the correct ctags.
    – ritchie
    Mar 11 at 19:32
  • @D.BenKnoble Sorry, I'm fairly new to asking questions on here. Are you a Moderator checking what answers are correct, incorrect, or misleading people? His answer worked for me. I didn't use his example, all I did was add --fields=+n to my code. If it will help you any I will give you my full command that I use for ctags. It is ctags --fields=+n -f $ebooks/vim/.tags --exclude=*.html --exclude=examples -R $ebooks/vim/ || echo 'Error in $ebooks/vim/' Then in my asciidoc documment I do :tag keyword and then to go to the next :tn.
    – ritchie
    Mar 11 at 20:07
  • @D.BenKnoble I don't think I would be of much help to you Ben. I'm not too familiar with ctags or tags in general. I just threw some ideas out there. Good luck :)
    – ritchie
    Mar 12 at 4:23
  • Figured it out; my ctags output was different than that shown in the original version of the other answer; this works fine for me, too (🎉)
    – D. Ben Knoble
    Mar 13 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.