Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

elixir - How to save/log the output of the iex shell to get persistent command history?

I just started working with Elixir and have been using the iex shell quite a bit. Is it possible for me to save / log a "session" to a file?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Erlang/OTP-20 and higher

Since Erlang/OTP-20rc2, Shell history is supported out of the box (although initially disabled by default) through a port of this library to the Erlang/OTP code base. Enable the shell in these versions by setting the shell_history kernel environment variable to enabled with export ERL_AFLAGS="-kernel shell_history enabled" added to your environment variables (see Configuration Options to see more options).

-- https://github.com/ferd/erlang-history

Trouble shooting

The history seems to be not updated (not written to the file)?

It seems that the process that is writing the history to the file does it asynchronously and it needs some time to do it before the IEx shell is closed. You need to wait a bit before you exit the shell (e.g. press <ctrl+>).


Pre Erlang/OTP-20:

I have found 2 ways to do it.

1. Erlang History

erlang-history (eh) is a tiny pair of files that can be used to patch an Erlang-OTP system to add support for history in the Erlang shell.

The history supported is the one available through up/down arrows on the keyboard.

Installation in Ubuntu Linux:

sudo su
cd /usr/local/src
git clone https://github.com/ferd/erlang-history.git
cd erlang-history
make install

Now every now started Erlang based REPL (and that is IEx) should use erlang-history.


2. rlwrap

As an alternative you can try a more generic REPL enhancer/fixer rlwrap which is a "readline wrapper":

...a small utility that uses the GNU readline library to allow the editing of keyboard input for any command.

rlwrap -a -A iex -S mix

(In case you are on Ubuntu Linux use: sudo apt-get install rlwrap)

It let's you add a lot more features to the REPL like e.g. the pipeto filter rlwrap -a -z pipeto iex that lets you pipe things to shell commands - very useful to read documentation i.e.: iex> h Stream | less (more)

Know downsides:

  • It breaks code completion (i.e. tab completion) in IEx

Why is this very useful feature - command history - not already included in Elixir/Erlang?


When using asdf see this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...