Export erlang shell history to text

I regularly use the erlang shell to play around with new topics.
From time to time I would like to use excerpts which are working in a regular module.
This is a bit cumbersome, I can either copy&paste line by line, or select a whole block from the shell and remove all the outputs in between the commands.
The persistent erlang shell is a disk_log, which is a set of binary files.

I came up with this short functions to export the whole history:

%%--------------------------------------------------------------------
%% @doc get all entries from the erlang shell history
%%      the erlang history is stored in a disk_log file, which is hard to read.
%%      this function extracts all entries from the current history and puts
%%      it into a text file
%% @param File filename for the text output file
%% @end
%%--------------------------------------------------------------------
get_history(File) ->
    {ok, F} = file:open(File, [write, raw]),
    L = '$#group_history',
    get_history(F, L, disk_log:chunk(L, start)).

get_history(F, _L, eof) ->
    file:close(F);
get_history(F, L, {Cont, Terms}) ->
    write_terms(F, Terms),
    get_history(F, L, disk_log:chunk(L, Cont)).

write_terms(F, Terms) ->
    lists:foreach(fun(I) ->
                          file:write(F, I)
                  end,
                  Terms).

but maybe there’s already a better solution, which I overlooked?

2 Likes

You can actually use a different module to save shell history, see -kernel shell_history my_module_name argument to erl: Erlang -- kernel

I specify this in my .zshrc file:

export ERL_AFLAGS="-kernel shell_history plain_text_history"
9 Likes

Q.E.D. :slight_smile:

2 Likes