How to automatically save iex session to text file?

Hi all,

How do I save a iex session (input and output) as a plain text file?

The BSD “script” command (make a typescript of terminal session) creates garbage in the file.
script [-adkpqr] [-F pipe] [-t time] [file [command ...]]

Original session:

riazor:~ fun2src$ script hello.txt
Script started, output file is hello.txt
bash-3.2$ iex
Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Interactive Elixir (1.8.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> "hello" <> "world"
"helloworld"
iex(2)> 
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
(v)ersion (k)ill (D)b-tables (d)istribution
^Cbash-3.2$ exit
Script done, output file is hello.txt
riazor:~ fun2src$

Output file content:

Script started on Tue Jul  9 15:42:02 2019
ESC[?1034hbash-3.2$ iex
Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Interactive Elixir (1.8.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> "hello" <> "world"
ESC[33mESC[32m"helloworld"ESC[0mESC[33mESC[0m
iex(2)> 
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
^Cbash-3.2$ exit

Script done on Tue Jul  9 15:42:40 2019
hello.txt (END)

Thanks in advance for any help you are able to provide.

1 Like

That’s not garbage but rather color code information for the terminal. ^.^

Set the terminal setting (and update the termcaps in the session!) to something black and white only and elixir won’t output color codes, or you can do it for a specific iex session by running IEx.configure(colors: [enabled: false]) within it, or you can put that line in a .iex.exs file in the current directory and it will automatically run it at iex launch.

For note, if you echo that output file contents to your shell (like just cat hello.txt) then you won’t see the color codes but rather you will see the colors, so it can be useful to keep them too.

5 Likes

Ok. Thanks a lot for your assistance! :smile:

1 Like