Sometimes you’re hacking away in iex, and you want to write a multi-line Elixir expression β€” maybe a small script, a complicated pipeline, or just something too big for a single line. Did you know you can drop into your favorite editor right from iex, write your code comfortably, and then execute it?

In your terminal, set the VISUAL environment variable to your editor of choice:

export VISUAL=nano    # or vim, nvim, code, emacs, etc.

You can also add this to your shell config (~/.bashrc, ~/.zshrc, etc.) to make it permanent.

Inside an iex shell, press:

ESCAPE + o

(That’s the Escape key, then the letter o.)

This will launch your configured editor in a temporary file. You can now write multi-line Elixir code just like you would in a .exs file.

Once you're done, save and quit the editor (:wq in Vim, Ctrl+O + Ctrl+X in Nano), and IEx will execute the code you just wrote.

Note: if you want to get the syntax highlighting correctly, then need need to associate tmp.*.erl with the Elixir language.

source