17

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

27

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
  • 2
    Maybe ${(kM)parameters:#[[:alpha:]_][[:alnum:]_]#} (with extendedglob) for only the parameters that are variables. Jun 22 '17 at 15:39
  • 1
    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 '17 at 19:59

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.