How do I save IEx history?
Asked Answered
R

4

49

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?

Raspings answered 30/7, 2017 at 21:54 Comment(0)
R
88

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
Raspings answered 30/7, 2017 at 21:54 Comment(10)
Just tried this out and it seems that history is not written correctly in some cases when exiting IEx by hitting Ctrl+C twice, which many people do. I had no problems when always terminating IEx with Ctrl+G Q Enter though, which is a cleaner way to exit IEx anyway.Wite
I have noticed that sometimes it has to do with how fast you quit. Might be a file write race condition of sorts?Raspings
Haha maybe it just takes longer to hit this insane key combo so I'm less likely to run into the problem ;-)Wite
On MacOS you have to create an empty history file, otherwise it does not work: $ touch ~/.iex_historyMetric
what's the advantage of ctrl-G Q enter over ctrl-C a enter?Expansible
@jaydel I guess it's the same as shutting down your computer by pressing the power button, vs using the "shut down" function of your OS: state is saved, doesn't end with an error etc.Castellano
AFAIC iex --erl "-kernel shell_history enabled" works, but ERL_AFLAGS="-kernel shell_history enabled" as environment variable doesn't, within a docker. If anyone has an idea, I'd be happy but I can live without it.Castellano
@AndreiSura On Linux too. So it looks like the file ~/.iex_history has to exist.Expectancy
@AugustinRiedinger hello two years later. I've added a docker compose example.Aplanospore
To find the location from iex directly the Elixir syntax is: :filename.basedir(:user_cache, "erlang-history")Merchantman
V
18

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.

Vladikavkaz answered 4/6, 2021 at 19:56 Comment(2)
Can confirm the accepted answer no longer works and the above did. Worth noting (as others mentioned) that if you use ctrl+c to quit (as I do), you'll need to use ctrl + g following by q and enterOldfangled
I can also confirm it and as of now the history is retained even when I use ctrl+c ctrl+c to exit on my Mac.Bemire
A
5

I'm using the oh-my-zsh, so I put on the vim ~/.zshrc:

# Enable history in IEX through Erlang(OTP)
export ERL_AFLAGS="-kernel shell_history enabled"

then source ~/.zshrc and now always load. Thank's @loeschg.

Aviva answered 7/12, 2018 at 15:16 Comment(0)
A
4

For docker compose, you will need to create a volume to save the history, and tell Erlang where it is via -kernel shell_history_path. The path needs to be an Erlang term, so make sure it's '"tripple quoted"':

version: '3'

services:
  elixir:
    environment:
      ERL_AFLAGS: -kernel shell_history enabled -kernel shell_history_path '"/somewhere/sensible"'
    volumes:
      - type: volume
        source: shell_history
        target: /somewhere/sensible

volumes:
  shell_history:
Aplanospore answered 20/12, 2021 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.