8

When I run ctags for a project written in bash, tags for functions are generated, but not for variables.
e.g. if I run ctags --list-kinds=sh

f functions

As compared to if I run the list for the "c" language alot more tag types get included.
Is it possible to generate tags for bash variables? or does anyone know of a custom regex that can added to ~/.ctags to enable this?

2
  • Possible lead to answer: stackoverflow.com/q/11740202/1600630
    – Cometsong
    Dec 13, 2015 at 20:43
  • @Cometsong thanks but even once you get ctags to recognise the filetype , the only tag type ctags is capable of producing for shell files - by default - is functions. essentially a regex rule needs to be written. the rules are pretty easy to write, which make me assume (possibly wrongly) there is some problem others havent been able to solve and hence no regex rule as of yet. Dec 13, 2015 at 20:53

3 Answers 3

4

based on the prompt from @Cometsong I added wrote the regex expression below and added to my ~/.ctags

--regex-sh=/^[ \t]*(local)?[ \t]*([A-Za-z0-9_-]+)=/\2/v,variable,variables/

So far it seems to be working quite nicely

3

A regex can be built to find variable declarations like [A-z]…=[A-z0-9]

Another for variable usage to include a \$ in the front

My own yaml-specific .ctags example is:

--regex-c=/^( YAML langdef config )/\1/k,t/
--langdef=yaml
--langmap=yaml:.yml.yaml
--regex-yaml=/^[ \t]*-[ \t]*.*:[ \t]*(.+)/\1/k,tasks/
--languages=+yaml

Modified for bash-isms of course.

--regex-sh=/^.*[ \t]*([A-Za-z][A-Za-z0-9_]*)=[".]*/\1/k,tasks/
--languages=+sh
2
  • thanks, why did you delete your regex? I think it needed a ^ plus needed the ability to capture strings inside quotes on the right side of = e.g. foo="hello world" Dec 13, 2015 at 21:17
  • See edit for example (currently untested).
    – Cometsong
    Dec 13, 2015 at 23:00
0

added export option to the expression from @the_velour_fog since sometimes the global option can also be set using export

--regex-sh=/^[ \t]*(local|export)?[ \t]*([A-Za-z0-9_-]+)=/\2/v,variable,variables/
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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