zimbatm/direnv — Issue #24: zsh and precmd()
Author: bmc
Created: 2012-01-05 04:19:34+00:00
State: open

`direnv` currently defines `precmd()`, as the means for hooking its behavior into *zsh*. The problem with that approach is that it doesn't play well with other *zsh* plugins that also define `precmd()`. Fortunately, there's a solution (which I just used, locally, to avoid bashing the `direnv`-installed `precmd()` hook). According to :

> For the functions below, it is possible to define an array that has the same name as the function with ‘`_functions`’ appended. Any element in such an array is taken as the name of a function to execute; it is executed in the same context and with the same arguments as the basic function. For example, if `$chpwd_functions` is an array containing the values `‘mychpwd’`, `‘chpwd_save_dirstack’`, then the shell attempts to execute the functions `‘chpwd’`, `‘mychpwd’` and `‘chpwd_save_dirstack’`, in that order.

So, instead of overwriting `precmd()`, and possibly trashing someone else's function, you could do something like this:

```shell
direnv_hook () {
eval `direnv export`
}
[[ -z $precmd_functions ]] && precmd_functions=()
precmd_functions=($precmd_functions direnv_hook)
```