iex: Elixir’s interactive shell

Most useful iex tips and tricks for everyday development

Kamil Lelonek
Kamil Lelonek  - Software Engineer

--

When you install Elixir, you will have three new executables available in your environment: iex, elixir, and elixirc. iex stands for and opens Elixir’s interactive shell (aka REPL), which we’ll discuss in this article.

REPL

Elixir Read-Eval-Print Loop is an interactive environment accessible in your console that reads and executes Elixir actions directly from there. From the command line, you will be able to play around and experiment with Elixir.

Once you run it, you’ll see the following:

➜  ~ iex
Erlang/OTP 24 [erts-12.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit] [dtrace]
Interactive Elixir (1.13.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

Depending on your computer (e.g., number of CPUs) and installed Erlang & Elixir versions, you may see some different numbers in your own console.

.iex.exs

Did you know you can create an .iex.exs file to evaluate its contents automatically in the shell’s context?

When starting, iex is looking for this file in the current working directory or the global one located at ~/.iex.exs, and load the first one found.

It can consist of the following:

# To have MyModule aliased:
alias MyApp.MyContext.MyModule
# To have variable declared:
my_variable = :my_value

When running IEx in the directory where such .iex.exs file is located gives you access to everything you have defined there.

iex -S mix

You can use a special way of starting IEx, which is useful when you want to play with mix projects in the Elixir shell.

When you run iex -S mix, the project is compiled (just like with mix compile). If this is successful, the shell is started, and all generated BEAM files (binaries that represent compiled modules) are accessible in load paths.

If you place .ex files under the lib folder (or any nested subdirectory there), they’ll be automatically included in the next build.

Quitting

Most developers don’t know how to exit VIM. Fortunately, in Elixir shell, it’s a little easier. Basically, you have two different options there:

➜  ~ iex
Erlang/OTP 24 [erts-12.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit] [dtrace]
Interactive Elixir (1.13.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
BREAK: (a)bort (A)bort with dump (c)ontinue (p)roc info (i)nfo
(l)oaded (v)ersion (k)ill (D)b-tables (d)istribution
^C%
➜ ~

The first of them is to double-press ctrl+c. Easy thing, but why twice? Probably to avoid possible misclicks and give you a chance to reconsider. IEx shows you even a nice message about what you can do later. Whenever you want to cancel quitting, just hit c key and enter.

The other, more brutal, way is to press ctrl+\. It will immediately close the REPL and moves you back to your system shell without any notice:

➜  ~ iex
Erlang/OTP 24 [erts-12.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit] [dtrace]
Interactive Elixir (1.13.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> %
➜ ~

Break

IEx allows you to break an expression into many lines, since this is common in Elixir. Imagine you enter something in like an opening string, a tuple, a map, a parenthesis, or anything which expects completion in the next line. iex will then look like this:

iex(1)> "
...(1)>
...(1)>
...(1)>

It will change its prompt to have an ellipsis and indicate it is waiting for you to finish the completion until it finds a closing tag (" in this case).

Every so often, you may find yourself trapped in the state of incomplete expression with no ability to terminate it (other than by exiting the shell). You want to cancel the input, but each time you press Enter, you get a new line expecting to continue the started input …(1)>.

To abort the input, type the following:

...(1)> #iex:break

This special break-trigger (#iex:break) will force the shell to break out of any pending expression and return to its normal state. It will cause IEx to raise a TokenMissingError and cancel waiting for any further input, taking you back to REPL. You can terminate any started expression like that when you want to execute a new command there.

Summary

In this article, I wanted to show you some interesting features of Elixir IEx that you won’t find by typing h in the terminal. My favorite and the most used command there is recompile/0 which recompiles the current project.

Subscribe to get the latest content immediately
https://tinyletter.com/KamilLelonek

If you have your own tips and tricks, feel free to share them with me and others in the comments section. See you in the next Elixir article!

--

--