{"id":13590675,"url":"https://github.com/Tarrasch/zsh-autoenv","last_synced_at":"2025-04-08T14:31:31.499Z","repository":{"id":10498883,"uuid":"12682411","full_name":"Tarrasch/zsh-autoenv","owner":"Tarrasch","description":"Autoenv for zsh","archived":false,"fork":false,"pushed_at":"2024-06-21T13:59:25.000Z","size":181,"stargazers_count":698,"open_issues_count":8,"forks_count":30,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-11-06T11:44:33.561Z","etag":null,"topics":["autoenv","zsh","zshrc"],"latest_commit_sha":null,"homepage":null,"language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tarrasch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-09-08T15:03:04.000Z","updated_at":"2024-10-19T17:20:12.000Z","dependencies_parsed_at":"2024-11-06T11:38:49.607Z","dependency_job_id":"f73a418d-afb9-48da-a4e3-6fc0639c8ceb","html_url":"https://github.com/Tarrasch/zsh-autoenv","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tarrasch%2Fzsh-autoenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tarrasch%2Fzsh-autoenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tarrasch%2Fzsh-autoenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tarrasch%2Fzsh-autoenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tarrasch","download_url":"https://codeload.github.com/Tarrasch/zsh-autoenv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247860643,"owners_count":21008325,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autoenv","zsh","zshrc"],"created_at":"2024-08-01T16:00:49.387Z","updated_at":"2025-04-08T14:31:31.121Z","avatar_url":"https://github.com/Tarrasch.png","language":"Perl","funding_links":[],"categories":["zsh","Perl","Plugins"],"sub_categories":["Zinit (née zplugin)"],"readme":"[![Build Status](https://travis-ci.org/Tarrasch/zsh-autoenv.svg?branch=master)](https://travis-ci.org/Tarrasch/zsh-autoenv)\n\n# Autoenv for Zsh\n\nzsh-autoenv automatically sources (known/whitelisted) `.autoenv.zsh` files,\ntypically used in project root directories.\n\nIt handles \"enter\" and leave\" events, nesting, and stashing of\nvariables (overwriting and restoring).\n\n## Requirements\n\n- Zsh version 4.3.10 or later.\n\n## Features\n\n- Support for enter and leave events, which can use the same file.\n  By default it uses `.autoenv.zsh` for entering, and `.autoenv_leave.zsh`\n  for leaving.\n- Interactively asks for confirmation / authentication before sourcing an\n  unknown `.autoenv.zsh` file, and remembers whitelisted files by their\n  hashed content.\n- Test suite.\n- Written in/for Zsh.\n\n### Variable stashing\n\nYou can use `autostash` in your `.autoenv.zsh` files to overwrite some\nvariable, e.g.  `$PATH`.  When leaving the directory, it will be automatically\nrestored.\n\n    % echo 'echo ENTERED; autostash FOO=changed' \u003e project/.autoenv.zsh\n    % FOO=orig\n    % cd project\n    Attempting to load unauthorized env file!\n    -rw-rw-r-- 1 user user 36 Mai  6 20:38 /tmp/project/.autoenv.zsh\n\n    **********************************************\n\n    echo ENTERED; autostash FOO=changed\n\n    **********************************************\n\n    Would you like to authorize it? (type 'yes') yes\n    ENTERED\n    project % echo $FOO\n    changed\n    % cd ..\n    % echo $FOO\n    orig\n\nThere is also `stash`, `unstash` and `autounstash`, in case you want to\nhave more control.\n\nThe varstash library has been taken from smartcd, and was optimized for Zsh.\n\n## Writing your .autoenv.zsh file\n\n### `autoenv_source_parent()`\n\nzsh-autoenv will stop looking for `.autoenv.zsh` files upwards after the first\none has been found, but you can use the function `autoenv_source_parent` to\nsource the next `.autoenv.zsh` file upwards the directory tree from there.\n\nThe function accepts an optional argument, which allows to stop looking before\nthe file system root is reached:\n\n```zsh\nautoenv_source_parent ../..\n```\n\n## Installation\n\nClone the repository and source it from your `~/.zshrc` file:\n\n    % git clone https://github.com/Tarrasch/zsh-autoenv ~/.dotfiles/lib/zsh-autoenv\n    % echo 'source ~/.dotfiles/lib/zsh-autoenv/autoenv.zsh' \u003e\u003e ~/.zshrc\n\n### Using [antigen](https://github.com/zsh-users/antigen)\n\n    antigen-bundle Tarrasch/zsh-autoenv\n\n### Using [zgen](https://github.com/tarjoilija/zgen)\n\nAdd the following to your `.zshrc` where you are loading your plugins:\n\n    zgen load Tarrasch/zsh-autoenv\n\n### Using [zplug](https://github.com/zplug/zplug)\n\nAdd the following to your `.zshrc` where you are loading your plugins:\n\n    zplug \"Tarrasch/zsh-autoenv\"\n\n## Configuration\n\nYou can use the following variables to control zsh-autoenv's behavior.\nAdd them to your `~/.zshrc` file, before sourcing/loading zsh-autoenv.\n\n### AUTOENV_FILE_ENTER\n\nName of the file to look for when entering directories.\n\nDefault: `.autoenv.zsh`\n\n### AUTOENV_FILE_LEAVE\n\nName of the file to look for when leaving directories.\nRequires `AUTOENV_HANDLE_LEAVE=1`.\n\nDefault: `.autoenv_leave.zsh`\n\n### AUTOENV_LOOK_UPWARDS\n\nLook for zsh-autoenv \"enter\" files in parent dirs?\n\nDefault: `1`\n\n### AUTOENV_HANDLE_LEAVE\n\nHandle leave events when changing away from a subtree, where an \"enter\"\nevent was handled?\n\nDefault: `1`\n\n### AUTOENV_DISABLED\n\n(Temporarily) disable zsh-autoenv. This gets looked at in the chpwd handler.\n\nDefault: 0\n\n### AUTOENV_DEBUG\n\nSet debug level. If enabled (\u003e 0) it will print information to stderr.\n\n- 0: no debug messages\n- 1: generic debug logging\n- 2: more verbose messages\n  - messages about adding/removing files on the internal stack\n- 3: everything\n  - sets xtrace option (`set -x`) while sourcing env files\n\nDefault: `0`\n\n## Usage\n\nzsh-autoenv works automatically once installed.\n\nYou can use ``autoenv-edit`` to edit the nearest/current autoenv files.\nIt will use ``$AUTOENV_EDITOR``, ``$EDITOR``, or ``vim`` for editing.\n\n## Helper functions\n\nThe following helper functions are available:\n\n### autoenv_append_path\n\nAppends path(s) to `$path` (`$PATH`), if they are not in there already.\n\n### autoenv_prepend_path\n\nPrepends path(s) to `$path` (`$PATH`), if they are not in there already.\n\n### autoenv_remove_path\n\nRemoves path(s) from `$path` (`$PATH`).\n\nReturns 0 in case `$path` has changed, 1 otherwise.\n\n## Recipes\n\n### Automatically activate Python virtualenvs\n\nGiven `AUTOENV_FILE_ENTER=.autoenv.zsh`, `AUTOENV_FILE_LEAVE=.autoenv.zsh` and\n`AUTOENV_HANDLE_LEAVE=1` the following script will activate Python virtualenvs\nautomatically in all subdirectories (`.venv` directories get used by\n[pipenv](https://github.com/kennethreitz/pipenv) with\n`PIPENV_VENV_IN_PROJECT=1`):\n\n```zsh\n# Environment file for all projects.\n#  - (de)activates Python virtualenvs (.venv) from pipenv\n\nif [[ $autoenv_event == 'enter' ]]; then\n  autoenv_source_parent\n\n  _my_autoenv_venv_chpwd() {\n    if [[ -z \"$_ZSH_ACTIVATED_VIRTUALENV\" \u0026\u0026 -n \"$VIRTUAL_ENV\" ]]; then\n      return\n    fi\n\n    setopt localoptions extendedglob\n    local -a venv\n    venv=(./(../)#.venv(NY1:A))\n\n    if [[ -n \"$_ZSH_ACTIVATED_VIRTUALENV\" \u0026\u0026 -n \"$VIRTUAL_ENV\" ]]; then\n      if ! (( $#venv )) || [[ \"$_ZSH_ACTIVATED_VIRTUALENV\" != \"$venv[1]\" ]]; then\n        unset _ZSH_ACTIVATED_VIRTUALENV\n        echo \"De-activating virtualenv: ${(D)VIRTUAL_ENV}\" \u003e\u00262\n\n        # Simulate \"deactivate\", but handle $PATH better (remove VIRTUAL_ENV).\n        if ! autoenv_remove_path $VIRTUAL_ENV/bin; then\n          echo \"warning: ${VIRTUAL_ENV}/bin not found in \\$PATH\" \u003e\u00262\n        fi\n\n        # NOTE: does not handle PYTHONHOME/_OLD_VIRTUAL_PYTHONHOME\n        unset _OLD_VIRTUAL_PYTHONHOME\n        # NOTE: does not handle PS1/_OLD_VIRTUAL_PS1\n        unset _OLD_VIRTUAL_PS1\n        unset VIRTUAL_ENV\n      fi\n    fi\n\n    if [[ -z \"$VIRTUAL_ENV\" ]]; then\n      if (( $#venv )); then\n        echo \"Activating virtualenv: ${(D)venv}\" \u003e\u00262\n        export VIRTUAL_ENV=$venv[1]\n        autoenv_prepend_path $VIRTUAL_ENV/bin\n        _ZSH_ACTIVATED_VIRTUALENV=\"$venv[1]\"\n      fi\n    fi\n  }\n  autoload -U add-zsh-hook\n  add-zsh-hook chpwd _my_autoenv_venv_chpwd\n  _my_autoenv_venv_chpwd\nelse\n  add-zsh-hook -d chpwd _my_autoenv_venv_chpwd\nfi\n```\n\n## Related projects\n\n- \u003chttps://github.com/direnv/direnv\u003e\n- \u003chttps://github.com/cxreg/smartcd\u003e\n- \u003chttps://github.com/kennethreitz/autoenv\u003e\n\n## History\n\nThis started as an optimized version of the bash plugin\n[autoenv](https://github.com/kennethreitz/autoenv) but for Zsh, and grew a lot\nof functionality on top of it (inspired by [smartcd]).\n\nThe code was initially based on\n[@joshuaclayton](https://github.com/joshuaclayton)'s dotfiles.\nIn September 2013 [@Tarrasch](https://github.com/Tarrasch) packaged it into a\nnice [antigen]-compatible unit with integration tests. Since November 2014,\n[@blueyed](https://github.com/blueyed) took over and added many nice\nfeatures, mainly inspired by [smartcd].\n\n[antigen]: https://github.com/Tarrasch/antigen-hs\n[smartcd]: https://github.com/cxreg/smartcd\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTarrasch%2Fzsh-autoenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTarrasch%2Fzsh-autoenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTarrasch%2Fzsh-autoenv/lists"}