Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 806
damianesteban Avatar asked Mar 15 '14 16:03

damianesteban


People also ask

How to log shell script output to file in Linux?

Here are the steps to log shell script output to file. 1. Create empty shell script Open terminal and run the following command to create an empty shell script. 2. Write Output to File Add the following lines at the top of your shell script to write the output to a file /output.txt. Replace this path with the file path of your choice.

How to log the output of SSH shell?

The answer to : "how to log the output of ssh shell?" is If you want to check the result of the cp cmmand on the remote server, I'd suggest running the if statement on the remote host as part of the command you send, immediately after running the cp command But i have few commands to run on this server-1 immediately after cp command.

How to save output of a shell script or command in Linux?

There will be times when you need to save the output to a file for future references. Now, you can surely copy and paste in Linux terminal but there are better ways to save the output of a shell script or command in Linux command line. Let me show them to you. You can use redirection in Linux for this purpose.

How to save command output and command errors in the same file?

You can save both the command output and command error in the same file using 2>&1 like this: Basically, 0 stands for standard input, 1 for standard output and 2 for standard error. Here, you are redirecting (>) standard error (2) to same address (&) as standard output (1).


2 Answers

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?

  • https://github.com/elixir-lang/elixir/issues/2945#issuecomment-66594851
  • https://github.com/elixir-lang/elixir/issues/1516#issuecomment-21772851

When using asdf see this.

like image 161
Szymon Jeż Avatar answered Nov 23 '22 18:11

Szymon Jeż


Not currently. You could probably write a small iex plugin to do this for you though. For example, I have the following file in ~/.iex.exs:

# .iex.exs defmodule IExHelpers do   def reload! do     Mix.Task.reenable "compile.elixir"     Mix.Task.run "compile.elixir"   end end  iex = IExHelpers   # $ iex -S mix # iex(2)> iex.reload! # :noop 

This recompiles the current project and reloads it while still inside a shell spawned with iex -S mix. You could probably write something to save the current shell's history to a file, and read it back in on startup, but I'm not sure where you would start with that.

like image 36
bitwalker Avatar answered Nov 23 '22 19:11

bitwalker