How Can We Clear the Screen in Iex on Windows

Your best option (if this is a real problem for you rather than an annoyance) is to use an alternate Windows shell that supports ANSI Escape Sequences. See this S O question for why you can't simply use ANSI Escape sequences in a Windows Cmd Shell. One command shell alternative that does support ANSI is ConEmu. Configuring ConEmu on your machine is left as an exercise for the reader.


Yes, we can't clear it on Windows as far as I know. If there is one escape that we can output to the IO device to clear screens on Windows, I would love to know and add this functionality to Windows too. :)


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


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 1
  • J is the CSI ED – Erase Display code with no arguments, by default clears the screen from the current cursor position
  • IEx.dont_display_result prevents the :ok result of IO.write from displaying after the screen is cleared

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

Tags:

Windows

Elixir