Set or unset environment variables from an "env" or dotenv file.
This package provides two interactive functions:
setenv-file
: Set all the environment variables defined in an env file.setenv-file-unset
: Unset all the environment variables defined in an env file.
When used interactively, each function prompts for a file. By default, the
prompt begins at setenv-file-dir
.
Copy setenv-file.el
to your machine, and load it:
(load "/path/to/setenv-file.el")
Or, using straight:
(straight-use-package
'(setenv-file :type git
:host github
:repo "cfclrk/setenv-file"))
Start by creating an env file in setenv-file-dir
(by default, ~/.env/
). For
example, create this file in ~/.env/foo
:
FOO=~/foo
BAR=$FOO/bar
ОФИС=ДОМ
BAZ=nosubst:FOO$BAR
Now, you can run:
M-x setenv-file
, which will prompt you for a file. All the environment variables defined in the file will be set.M-x setenv-file-unset
, which will prompt you for a file. All the environment variables defined in the file will be unset.
To set env variables defined in ~/.env/foo
:
(setenv-file (expand-file-name "~/.env/foo"))
Or, if you have a string instead of a file:
(setenv-file-str "FOO=foo\nBAR=bar")
The example below shows a convenient way to declare and set environment
variables in an org
document:
#+NAME: env
| Var | Value |
|------+-----------------|
| FOO | ~/foo |
| BAR | $FOO/bar |
| ОФИС | ДОМ |
| BAZ | nosubst:FOO$BAR |
#+begin_src emacs-lisp :var env=env
(setenv-file-set-pairs env)
#+end_src
Each line in the file should be in a KEY=VALUE
format, with one entry per
line. This package does not invoke a shell to interpret the file, so most
shell-isms will not work. However, the env file may:
- Use existing environment variables
- Define an environment variable and use it in successive lines
- A
~
is expanded if it is the first character in the value - If a value starts with `nosubst:`, no variable substitution will be
performed. You need this if there is a literal
$
in the value.
make dep
: Install dependenciesmake test
: Run unit tests (you must runmake dep
first!)make doc
: Runs an org export ondoc/doc.org
which creates:README.md
- The
;;; Commentary
section insetenv-file.el
- Package texinfo (
.texi
and.info
) indoc/