In zsh, where are precmd_functions defined

functionzsh

If in zsh, I type set and see precmd_functions=(_precmd_function_dostuff _precmd_function_domore).

Where are _precmd_function_dostuff and _precmd_function_domore defined (i.e. are they defined in a file? which file?)?

I can type functions to see the definitions of _precmd_function_dostuff and _precmd_function_domore, but this doesn't tell me where they are defined.

Best Answer

In zsh 5.3 or above,

type _precmd_function_domore

should return something like

_precmd_function_domore is a shell function from /usr/local/etc/zshrc.d/80-PetaLinux

With zsh 5.4 or above, you can also use:

echo $functions_source[_precmd_function_domore]

When you run zsh with the xtrace option (like with zsh -x), it writes debugging information on stderr that shows every command it runs (not function definitions though). You can modify the $PS4 variable (the prompt variable used for the xtrace output, see info zsh PS4) so it gives you more information like for each command that it runs, from which file and on each line the command was read from.

PS4='+%x:%I> ' zsh -x 2> >(grep precmd_func)

Would run a new zsh interactive shell instance, with stderr filtered by grep to show the lines that contain precmd_func.

Or with zsh, you can invoke that _precmd_function_domore function under xtrace and with %x:%I in $PS4 to see where the function definition was read from:

$ grep -n precmd ~/.zshrc
192:precmd_foo() echo foo
$ (PS4='+%x:%I> '; set -x; precmd_foo)
+zsh:2> precmd_foo
+/home/stephane/.zshrc:194> echo foo
foo

(note the off-by-two line number here though).

Related Question