我如何 reset/clear erlang 终端

How do I reset/clear erlang terminal

我正在尝试重置提示,忘记所有变量并从第 1 行开始提示>
我知道以下内置函数

f().                      %% forget all
io:format("\e[H\e[J").    %% "clear screen" and moving cursor to the begin of the line

但是当我写下面的命令时,它确实忘记了所有变量,但它不会 "reset" 屏幕,只是清除屏幕,就像终端中的 clear 命令.

在 Linux 中,我只是键入 reset,但我找不到等效的命令或内置函数供 erlang 执行此操作。

我也试过 io:format(os:cmd("reset")). 但我收到错误消息。

我现在的解决方案是退出 erlang 终端,然后重新打开它,但我相信有更简单的方法可以做到这一点。

您在类 Unix 系统上使用的大多数终端都支持 VT100 hardware-reset 转义序列。您可以像这样在您的程序中使用它:

io:format("\ec").

而不是

io:format("\e[H\e[J").    %% "clear screen" and moving cursor to the begin of the line

虽然两者都做也没什么坏处。而且,它不会影响你的变量,所以你仍然会做

f().                      %% forget all

结合这些:

f().                      %% forget all
io:format("\ec").
io:format("\e[H\e[J").    %% "clear screen" and moving cursor to the begin of the line

应该是你想要的。

有点棘手的方法是先按 ctrl-g 然后按 s、[=12 开始一个新的 shell =]

$ erl
Erlang/OTP 18 [erts-7.3] [source-d2a6d81] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> A = 1.
1
2>
User switch command
 --> s
 --> c
Eshell V7.3  (abort with ^G)
1> A.
* 1: variable 'A' is unbound
2>

当然,这并不能清除屏幕。为此,您必须使用自己的控制台机制(我在 OSX 上使用 iTerm,为此我只点击了 cmd-k

正如@Brujo Benavides 之前提到的那样。这样做的方法是退出当前的 erl shell 并开始一个新的 .您可以使用不太棘手的 halt(). 函数。

清除erl shell

io:format(os:cmd(clear)).

还是做一个更彻底的清除。这将清除您终端的缓冲区:

io:format("33").

相当于以下任一 shell 命令:

printf "33"
tput reset

后者需要您安装 ncurses