Loading .erlang from the working directory
In the previous post, I wrote about customizing the Erlang shell
prompt by using the
.erlang file. Erlang looks for this file in
$HOME (I’m simplifying here); it doesn’t look in the
current working directory. How can we fix that?
I like the way that Elixir looks for an
.iex.exs file in the current directory. But Erlang (since OTP-21.0) doesn’t
look for the
.erlang file in the current directory.
The documentation says:
When Erlang/OTP is started, the system searches for a file named
.erlangin the user’s home directory and then
On Linux, that usually resolves to
You can tell the shell to load another
.erlang file by using the
c:erlangrc/1 function; it takes a list of
directories to look in. For example:
We can do that when starting
erl -run c erlangrc .
(Thanks to Ruan for the tip)
How can we do that automatically?
My first thought was something like this:
ERL_ZFLAGS="-run c erlangrc ." erl
…which works fine. You could set
ERL_ZFLAGS in your profile, even. As far as I can tell, it doesn’t break anything.
It even loads the default
.erlang file from
So we’re done, right? No.
If you’re using direnv and kerl, you could put it in your
use erlang OTP-25.2.1 export ERL_ZFLAGS="-run c erlangrc ."
Because direnv prompts you to approve each
.envrc file, we can use this to prevent automatically running
until we’ve checked it.
If you’re using
erlang.mk, you could use the
SHELL_OPTS = -run c erlangrc .
I couldn’t find a clean way to have this work in
rebar3 shell. Look at this Github
issue for inspiration.