28

I need a way to list the names (without the body/content/value) of aliases, functions, variables, arrays in zsh. Optimally it should behave like compgen:

compgen -a # will list all the aliases you could run.
compgen -A function # will list all the functions you could run.
compgen -A variable # will list all the variables defined.

Background

I need this to develop env_parallel.zsh: https://www.gnu.org/software/parallel/env_parallel.html

1 Answer 1

41

Aliases and functions are contained in aliases and functions, and one merely needs to print the keys of such. "Variables and arrays" is trickier; parameters may suffice?

print -rl -- ${(k)aliases} ${(k)functions} ${(k)parameters}

(You may also need builtins, commands, and perhaps other things listed from print -l ${(k) and then mashing tab, assuming completion is enabled.)

2
  • 4
    Maybe ${(kM)parameters:#[[:alpha:]_][[:alnum:]_]#} (with extendedglob) for only the parameters that are variables. Jun 22, 2017 at 15:39
  • 2
    for k in ${(koM)parameters:#[[:alpha:]][[:alnum:]_]#}; do; [[ ${(M)parameters[$k]:#association*} ]] || continue; print -- "$k"; done (extendedglob needs to be set) should give you a short list of variables to use depending on what data you are looking for. Changing [[:alpha:]] to [[:alpha:]_] will include associative arrays(or variables) which start with an underscore. Changing association* to scalar* or array* or integer* or *export* will give you the results of those types. "Variables and arrays" take a bit more work.
    – Friartek
    Oct 8, 2017 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .