10

Please how can we clear the screen in Iex on Windows

Documented method in Iex help does not work:

  • clear/0 — clears the screen

This StackOverflow Answer also does not work in Windows.

Community
  • 1
  • 1
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157

5 Answers5

14

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

ajukraine
  • 561
  • 9
  • 27
  • 2
    You may want to submit an issue on Elixir's github to make them aware that this is now supported under Windows 10. They may want to alter the behavior. – Onorio Catenacci Dec 20 '16 at 13:01
  • The problem is that ANSI support in native terminal is implemented only in specific Windows 10 update, and I'm not sure it's ok to enable it by default for all Windows installations. I think, it should be up to user to enable ANSI in `iex` – ajukraine Dec 22 '16 at 10:44
  • I've just checked closed issues on their's github. They already have this ANSI support detection in master branch. – ajukraine Dec 22 '16 at 10:49
6

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.

jwfearn
  • 28,781
  • 28
  • 95
  • 122
3

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

José Valim
  • 50,409
  • 12
  • 130
  • 115
3

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.

Community
  • 1
  • 1
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
  • 2
    The linked answer suggests using ANSICON, which I think would be simpler than ConEmu. FYI, ConEmu is a terminal/console replacement, not a shell, and the windows console (conhost.exe) that it partially replaces is also not a shell. cmd.exe is a shell, but it has nothing to do with rendering the console window. Like any console program it gets a handle to the attached conhost.exe instance, and its standard handles for `StandardInput`, `StandardOutput`, and `StandardError` default to the console's input buffer and screen buffer. – Eryk Sun Jun 18 '15 at 23:32
  • @eryksun You're correct--I'm not being very precise in terms of what's a shell and what's a terminal/console. – Onorio Catenacci Jun 19 '15 at 12:23
  • 1
    I use `iex` in ConEmu and I still get the "ANSI escape codes are not enabled" message even though they clearly are (I have a color iex prompt.) – jwfearn Jun 25 '16 at 15:47
  • 1
    I also get the "ANSI escape codes are not enabled" message in console, and GitBash. – jwfearn Jun 25 '16 at 15:56
  • @jwfearn Are you getting that message from Iex? Iex checks the OS so having Conemu makes no difference whatever. – Onorio Catenacci Jun 25 '16 at 15:59
  • @OnorioCatenacci, yes that message is from `iex`. It seems on Windows, `iex` just doesn't do ANSI. That's not a bad default since it doesn't always work. Unfortunately I haven't yet found a way to override this default behavior. – jwfearn Jun 25 '16 at 16:05
2

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

Dylan
  • 1,335
  • 8
  • 21