With IEx (Elixir's REPL), I'd like to be able to save my command history.
For example:
I can open up a new IEx session and execute a command. After executing the command I can press the up arrow and have my last-command pre-populated. After closing IEx and re-opening, I'd like to have access to my last commands.
Is there a way to do this?
Ctrl-C. What I'm used to do is to hit Ctrl-C twice. It has the same effect as the abort command. The Break command can be triggered from any running Elixir code and not only iex .
The command to start IEx is… iex . If you press c , you will remain in IEx. If you press a , it will abort the shell and exit.
For Erlang/OTP 20
This is built-in (from https://hexdocs.pm/iex/IEx.html#module-shell-history)
From Erlang/OTP 20, it is possible to get shell history by passing some flags that enable it in the VM. This can be done on a per-need basis when starting IEx:
iex --erl "-kernel shell_history enabled"
If you would rather enable it on your system as a whole, you can use the
ERL_AFLAGS
environment variable and make sure that it is set accordingly on your terminal/shell configuration.On Linux [and macOS]:
export ERL_AFLAGS="-kernel shell_history enabled"
On Windows:
set ERL_AFLAGS "-kernel shell_history enabled"
To show where the history file is located, run the following code from erl (Mac OS example value shown):
1> filename:basedir(user_cache, "erlang-history") "/Users/your.username/Library/Caches/erlang-history"
To set the file to a different location, use the shell_history_path /path/to/history-file
option from the erlang docs (compatible with Elixir/iex):
export ERL_AFLAGS="-kernel shell_history_path '\"$HOME/.erlang-history\"'"
For Erlang/OTP 19 and below
Try using https://github.com/ferd/erlang-history
> git clone https://github.com/ferd/erlang-history.git > cd erlang-history > sudo make install # may not need sudo depending on installation
I don't know if things changed at some point, but I found the above did not work. After looking at the man page for iex, I noticed that it needed to be
export ELIXIR_ERL_OPTIONS="-kernel shell_history enabled"
(note the additional ELIXIR). Perhaps the original solution was cogent was relevant for erl (I found it worked for that), but iex added the qualifier? Since the original question was for iex though, figured it should be updated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With