简体   繁体   中英

How do I reset/clear erlang terminal

I'm trying to get the prompt reset, forget all the variables and start the prompt from line 1>
I'm aware of the following built-in functions

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

but when I'm writing the following commands, it does forget all the variables, but it doesn't "reset" the screen, just clear the screen, like clear command in the terminal.

in Linux, I just type reset , but I couldn't find equivalent command or built in function for erlang to do that.

I also tried io:format(os:cmd("reset")). but I received error.

my solution for now is to quit the erlang terminal, and reopen it again, but I'm sure there is much easier ways to do that.

Most of the terminals you would use on a Unix-like system support the VT100 hardware-reset escape sequence. You could use that in your program like this:

io:format("\ec").

That is instead of

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

although it would not hurt to do both. Also, it does not affect your variables, so you would still do

f().                      %% forget all

Combining these:

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

should be what you intended.

A somewhat tricky way to do it would be to just start a new shell by pressing ctrl-g and then s , c

$ 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>

Of course that doesn't clear the screen. You have to use your own console mechanisms for that (I use iTerm on OSX and I just hit cmd-k for that)

清除erl shell

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

As @Brujo Benavides mentioned earlier . The way to do that is to exit the current erl shell and start a new . You can use the halt(). function which is less tricky .

Or do a more thorough clear. This will clear your terminal's buffer:

io:format("\033\143").

Which equals to either of the following shell commands:

printf "\033\143"
tput reset

The latter requires you have ncurses installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM