1 [![Build Status](https://travis-ci.org/Tarrasch/zsh-autoenv.svg?branch=master)](https://travis-ci.org/Tarrasch/zsh-autoenv)
5 zsh-autoenv automatically sources (known/whitelisted) `.autoenv.zsh` files,
6 typically used in project root directories.
8 It handles "enter" and leave" events, nesting, and stashing of
9 variables (overwriting and restoring).
13 - Support for enter and leave events, which can use the same file.
14 By default it uses `.autoenv.zsh` for entering, and `.autoenv_leave.zsh`
16 - Interactively asks for confirmation / authentication before sourcing an
17 unknown `.autoenv.zsh` file, and remembers whitelisted files by their
24 You can use `autostash` in your `.autoenv.zsh` files to overwrite some
25 variable, e.g. `$PATH`. When leaving the directory, it will be automatically
28 % echo 'echo ENTERED; autostash FOO=changed' > project/.autoenv.zsh
31 Attempting to load unauthorized env file!
32 -rw-rw-r-- 1 user user 36 Mai 6 20:38 /tmp/project/.autoenv.zsh
34 **********************************************
36 echo ENTERED; autostash FOO=changed
38 **********************************************
40 Would you like to authorize it? (type 'yes') yes
48 There is also `stash`, `unstash` and `autounstash`, in case you want to
51 The varstash library has been taken from smartcd, and was optimized for Zsh.
53 ## Writing your .autoenv.zsh file
55 ### `autoenv_source_parent()`
57 zsh-autoenv will stop looking for `.autoenv.zsh` files upwards after the first
58 one has been found, but you can use the function `autoenv_source_parent` to
59 source the next `.autoenv.zsh` file upwards the directory tree from there.
61 The function accepts an optional argument, which allows to stop looking before
62 the file system root is reached:
65 autoenv_source_parent ../..
70 Clone the repository and source it from your `~/.zshrc` file:
72 % git clone https://github.com/Tarrasch/zsh-autoenv ~/.dotfiles/lib/zsh-autoenv
73 % echo 'source ~/.dotfiles/lib/zsh-autoenv/autoenv.zsh' >> ~/.zshrc
75 ### Using [antigen](https://github.com/zsh-users/antigen)
77 antigen-bundle Tarrasch/zsh-autoenv
79 ### Using [zgen](https://github.com/tarjoilija/zgen)
81 Add the following to your `.zshrc` where you are loading your plugins:
83 zgen load Tarrasch/zsh-autoenv
85 ### Using [zplug](https://github.com/zplug/zplug)
87 Add the following to your `.zshrc` where you are loading your plugins:
89 zplug "Tarrasch/zsh-autoenv"
93 You can use the following variables to control zsh-autoenv's behavior.
94 Add them to your `~/.zshrc` file, before sourcing/loading zsh-autoenv.
96 ### AUTOENV_FILE_ENTER
98 Name of the file to look for when entering directories.
100 Default: `.autoenv.zsh`
102 ### AUTOENV_FILE_LEAVE
104 Name of the file to look for when leaving directories.
105 Requires `AUTOENV_HANDLE_LEAVE=1`.
107 Default: `.autoenv_leave.zsh`
109 ### AUTOENV_LOOK_UPWARDS
111 Look for zsh-autoenv "enter" files in parent dirs?
115 ### AUTOENV_HANDLE_LEAVE
117 Handle leave events when changing away from a subtree, where an "enter"
124 (Temporarily) disable zsh-autoenv. This gets looked at in the chpwd handler.
130 Set debug level. If enabled (> 0) it will print information to stderr.
132 - 0: no debug messages
133 - 1: generic debug logging
134 - 2: more verbose messages
135 - messages about adding/removing files on the internal stack
137 - sets xtrace option (`set -x`) while sourcing env files
143 zsh-autoenv works automatically once installed.
145 You can use ``autoenv-edit`` to edit the nearest/current autoenv files.
146 It will use ``$AUTOENV_EDITOR``, ``$EDITOR``, or ``vim`` for editing.
150 ### Automatically activate Python virtualenvs
152 Given `AUTOENV_FILE_ENTER=.autoenv.zsh`, `AUTOENV_FILE_LEAVE=.autoenv.zsh` and
153 `AUTOENV_HANDLE_LEAVE=1` the following script will activate Python virtualenvs
154 automatically in all subdirectories (`.venv` directories get used by
155 [pipenv](https://github.com/kennethreitz/pipenv) with
156 `PIPENV_VENV_IN_PROJECT=1`):
159 # Environment file for all projects.
160 # - (de)activates Python virtualenvs (.venv) from pipenv
162 if [[ $autoenv_event == 'enter' ]]; then
163 autoenv_source_parent
165 _my_autoenv_venv_chpwd() {
166 if [[ -z "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
170 setopt localoptions extendedglob
172 venv=(./(../)#.venv(NY1:a))
174 if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
175 if ! (( $#venv )) || [[ "$_ZSH_ACTIVATED_VIRTUALENV" != "$venv[1]" ]]; then
176 unset _ZSH_ACTIVATED_VIRTUALENV
177 echo "De-activating virtualenv: ${(D)VIRTUAL_ENV}" >&2
182 if [[ -z "$VIRTUAL_ENV" ]]; then
183 if (( $#venv )); then
184 echo "Activating virtualenv: ${(D)venv}" >&2
185 source $venv[1]/bin/activate
186 _ZSH_ACTIVATED_VIRTUALENV="$venv[1]"
190 autoload -U add-zsh-hook
191 add-zsh-hook chpwd _my_autoenv_venv_chpwd
192 _my_autoenv_venv_chpwd
194 add-zsh-hook -d chpwd _my_autoenv_venv_chpwd
200 - <https://github.com/direnv/direnv>
201 - <https://github.com/cxreg/smartcd>
202 - <https://github.com/kennethreitz/autoenv>
206 This started as an optimized version of the bash plugin
207 [autoenv](https://github.com/kennethreitz/autoenv) but for Zsh, and grew a lot
208 of functionality on top of it (inspired by [smartcd]).
210 The code was initially based on
211 [@joshuaclayton](https://github.com/joshuaclayton)'s dotfiles.
212 In September 2013 [@Tarrasch](https://github.com/Tarrasch) packaged it into a
213 nice [antigen]-compatible unit with integration tests. Since November 2014,
214 [@blueyed](https://github.com/blueyed) took over and added many nice
215 features, mainly inspired by [smartcd].
217 [antigen]: https://github.com/Tarrasch/antigen-hs
218 [smartcd]: https://github.com/cxreg/smartcd