function __env_per_dir --on-variable PWD --description "Apply per-directory env vars from ~/.config/fish/.env-per-dir.conf" set -l file ~/.config/fish/.env-per-dir.conf if set -q env_per_dir_file; and string length -q -- $env_per_dir_file set file $env_per_dir_file end test -f "$file"; or return 0 set -g __env_per_dir_managed set -g __env_per_dir_wanted while read -l line string match -rq -- '^[[:space:]]*#' "$line"; and continue string match -q -- '' "$line"; and continue set -l parts (string split --max 1 ' => ' -- "$line") test (count $parts) -eq 2; or continue set -l assignments (string split ' ' -- $parts[1]) set -l folders (string split ';' -- $parts[2]) for a in $assignments set -l kv (string split --max 1 '=' -- "$a") test (count $kv) -eq 2; or continue if not contains -- $kv[1] $__env_per_dir_managed set -a __env_per_dir_managed $kv[1] end end set -l matched 0 for d in $folders if string match -q -- "$d" "$PWD"; or string match -q -- "$d/*" "$PWD" set matched 1 end end if test $matched -eq 1 for a in $assignments set -l kv (string split --max 1 '=' -- "$a") test (count $kv) -eq 2; or continue set -a __env_per_dir_wanted $kv[1] set -gx $kv[1] $kv[2] end end end < "$file" for name in $__env_per_dir_managed if not contains -- $name $__env_per_dir_wanted set -e "$name" end end end