Tips and Tricks for IEx

September 01, 2017
Robert Beene
Managing Partner

Tips and Tricks for IEx

When I first started learning Elixir, one of the first things I learned is how to get into the command line interface via IEx. Here are the things I wish I knew then that I know now.

Access to the result of prior statements

We’ve all been there. You just executed a function to do something important. Unfortunately, you forgot to set the result to a variable and so you’re stuck repeating the line and adding a variable.

No more.

Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> a = "My first statement"
"My first statement"
iex(2)> b = "My second statement"
"My second statement"
iex(3)> c = "My third statement"
"My third statement"
iex(4)> v(2)
"My second statement"
iex(5)> v(1)
"My first statement"
iex(6)> v(-4)
"My second statement"

By using v(), we can grab the prior statement. Let’s say you really screwed things up though and you need something 2 statements back. No problem!

v() takes an optional argument (defaulted to -1). If this argument is positive, it will return the value of the nth expression in your history. If you want to go back relative to your current spot in the history, use a negative number.

Shell History

If you’re on OTP20, you can get shell history! Even between sessions, your up arrow will bring back previous commands. There are a couple of ways to accomplish this.


  1. iex --erl "-kernel shell_history enabled" - parameters when starting it up

All Day Every Day

  1. Linux - export ERL_AFLAGS="-kernel shell_history enabled"
  2. Windows - set ERL_AFLAGS "-kernel shell_history enabled"

Set the above in your environment variables and you’ll be set!

Fat Finger Escape

Nothing is more frustrating than write a long statement and hitting enter only to find you made an typo resulting in IEx thinking you’re writing a multi-line statement. Perhaps it was the wrong closing quotation or parentheses. Either way, it doesn’t matter. We just want to break out of the statement and move on with life.

Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> "I've messed up my statement and I can't break out'
...(1)> #iex:break
** (TokenMissingError) iex:1: incomplete expression


#iex:break is the friend who gets you out of trouble, no questions asked.

Alias Déjà vu

Here’s the common situation. You need to debug something in IEx which requires aliasing five things without typing your way into carpal tunnel. It’s almost always the same five things. Can’t we just configure these when getting into an IEx session?

Yes…yes you can.

Example.Repo.get(Example.Account.User, 1) can become Repo.get(User, 1) every time you open an IEx session for a given project.

All you need is to add an .iex.exs file with the appropriate aliases.

alias Example.Repo
alias Example.Account.User

Next time you launch IEx in that directory, your aliases will be ready.


Debugging is a part of any developer’s life. Elixir opens the door with pry. When you want to see what the state of a function is at a particular line, simply drop in the following and you’ll be dropped in there at execution.

require IEx; IEx.pry

Keep in mind that in order for this to hit, you’ll need to be running the process within an IEx session. For example, in a Phoenix app you would need to start things up with iex -S mix phx.server

The same goes for running tests.

iex -S mix test

FYI: the -S option is for scripts.


If you’ve known about pry, you’ve probably made the mistake of leaving one in while making a commit to fix a bug. Wouldn’t it be great if we could just drop these in to a IEx session without changing the code? With OTP20, you can do just that.

iex(2)> IEx.break!(User, :list_users, 3)
iex(3)> User.list_users(user, "admin", %{})
Break reached: Example.Account.User.list_users/3 (lib/example/account/user.ex:27)

   25:   end
   27:   def list_users(_user, "admin", params) do
   28:     User
   29:     |> filter_by_params(params)


Simply write IEx.break! with 3 arguments. The module, function, and arity. Anytime this hits, you’ll fall into a pry.

Note: you will not have access to aliases and imports from the source code unlike IEx.pry

You can read about some additional options around breakpoints here.

Have a tip that we missed? Tweet us @echobind!