Please how can we clear the screen in Iex
on Windows
Documented method in Iex help does not work:
clear/0
— clears the screenThis StackOverflow Answer also does not work in Windows.
Interactive Elixir - press Ctrl+C to exit (type h() ENTER for help) iex(1)> [1, 2, 3, 4, 5] [1, 2, 3, ...]
The command to start IEx is… iex . If you press c , you will remain in IEx. If you press a , it will abort the shell and exit.
I've discovered it's possible, because native terminal in Windows 10 supports ANSI colors and escape sequences. The only thing is to enabled this in iex
shell.
According to https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/io/ansi.ex#L41 this option is configurable. As a quick solution just type in your iex
session following code:
Application.put_env(:elixir, :ansi_enabled, true)
In order to make it permanent, you can configure iex
shell in ~/.iex.exs
file (https://hexdocs.pm/iex/IEx.html#module-configuring-the-shell). Just paste following into the file:
IEx.configure [colors: [enabled: true]]
Add the following to ~/.iex.exs
in your home directory -- If the file doesn't exist, create it and add the following.
Application.put_env(:elixir, :ansi_enabled, true)
Edit: Note that you'll need a term that supports this, ConEmu, Cmder, etc..
You can use ANSI codes directly in iex
on Windows with consoles that support them (like ConEmu or Windows 10 console.)
This will clear the screen in iex
:
iex> IO.write "\e[H\e[J"; IEx.dont_display_result
Explanation:
IO.write
outputs to the console without a newline\e[
is the prefix for ANSI CSI codes
H
is the CSI CUP – Cursor Position code with no arguments, by default moves cursor to row 1, column 1J
is the CSI ED – Erase Display code with no arguments, by default clears the screen from the current cursor positionIEx.dont_display_result
prevents the :ok
result of IO.write
from displaying after the screen is clearedYou can also clear the screen using IO.ANSI
rather than raw escape codes:
iex> IO.write [IO.ANSI.home, IO.ANSI.clear]; IEx.dont_display_result
This is basically how clear/1
is implemented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With