Tips and Tricks for IEx

Robert Beene
Echobind

--

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.

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.

Temporary

  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.

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

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

Pry

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.

IEx.break!

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.

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!

--

--

Robert Beene
Echobind

Helping mentor developers, working with startups. Rails/Elixir/Python