如何保存/记录iex shell的输出以获取持久的命令历史记录?

时间:2022-10-12 13:54:00

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?

我刚开始使用Elixir并且一直在使用iex shell。我可以将“会话”保存/记录到文件中吗?

Thank you.

谢谢。

2 个解决方案

#1


9  

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插件为你做这个。例如,我在〜/ .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.

这将重新编译当前项目并重新加载它,同时仍然在使用iex -S mix生成的shell中。您可能会编写一些内容来将当前shell的历史记录保存到文件中,并在启动时将其读回来,但我不确定您从哪里开始。

#2


40  

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).

从Erlang / OTP-20rc2开始,Shell历史记录支持开箱即用(虽然最初默认禁用),通过此库的端口到Erlang / OTP代码库。通过将shell_history内核环境变量设置为启用并启用导出ERL_AFLAGS =“ - kernel shell_history enabled”添加到环境变量(请参阅配置选项以查看更多选项),在这些版本中启用shell。

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

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


Pre Erlang/OTP-20:

I have found 2 ways to do it.

我找到了2种方法。

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.

erlang-history(eh)是一对小文件,可用于修补Erlang-OTP系统以在Erlang shell中添加对历史记录的支持。

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

支持的历史记录是通过键盘上的上/下箭头可用的历史记录。

Installation in Ubuntu Linux:

在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.

现在每个基于Erlang的REPL(也就是IEx)都应该使用erlang-history。


2. rlwrap

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

作为替代方案,您可以尝试更通用的REPL增强器/修复程序rlwrap,这是一个“readline包装器”:

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

...一个小实用程序,它使用GNU readline库来允许编辑任何命令的键盘输入。

rlwrap -a -A iex -S mix

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

(如果你在Ubuntu Linux上使用: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)

它让你可以为REPL添加更多功能,例如: pipeto过滤器rlwrap -a -z pipeto iex,它允许你将东西管道输出到shell命令 - 阅读文档非常有用,即:iex> h Stream |更少)

Know downsides:

知道缺点:

  • It breaks code completion (i.e. tab completion) in IEx
  • 它打破了IEx中的代码完成(即标签完成)

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

为什么这个非常有用的功能 - 命令历史记录 - 还没有包含在Elixir / Erlang中?


When using asdf see this.

使用asdf时看到这个。

#1


9  

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插件为你做这个。例如,我在〜/ .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.

这将重新编译当前项目并重新加载它,同时仍然在使用iex -S mix生成的shell中。您可能会编写一些内容来将当前shell的历史记录保存到文件中,并在启动时将其读回来,但我不确定您从哪里开始。

#2


40  

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).

从Erlang / OTP-20rc2开始,Shell历史记录支持开箱即用(虽然最初默认禁用),通过此库的端口到Erlang / OTP代码库。通过将shell_history内核环境变量设置为启用并启用导出ERL_AFLAGS =“ - kernel shell_history enabled”添加到环境变量(请参阅配置选项以查看更多选项),在这些版本中启用shell。

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

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


Pre Erlang/OTP-20:

I have found 2 ways to do it.

我找到了2种方法。

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.

erlang-history(eh)是一对小文件,可用于修补Erlang-OTP系统以在Erlang shell中添加对历史记录的支持。

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

支持的历史记录是通过键盘上的上/下箭头可用的历史记录。

Installation in Ubuntu Linux:

在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.

现在每个基于Erlang的REPL(也就是IEx)都应该使用erlang-history。


2. rlwrap

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

作为替代方案,您可以尝试更通用的REPL增强器/修复程序rlwrap,这是一个“readline包装器”:

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

...一个小实用程序,它使用GNU readline库来允许编辑任何命令的键盘输入。

rlwrap -a -A iex -S mix

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

(如果你在Ubuntu Linux上使用: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)

它让你可以为REPL添加更多功能,例如: pipeto过滤器rlwrap -a -z pipeto iex,它允许你将东西管道输出到shell命令 - 阅读文档非常有用,即:iex> h Stream |更少)

Know downsides:

知道缺点:

  • It breaks code completion (i.e. tab completion) in IEx
  • 它打破了IEx中的代码完成(即标签完成)

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

为什么这个非常有用的功能 - 命令历史记录 - 还没有包含在Elixir / Erlang中?


When using asdf see this.

使用asdf时看到这个。