How to quit the Elixir shell (IEx)?

  • George Guimarães
  • March 3rd, 2016
  • elixir, iex

Okay, you’ve been delving into Elixir. That’s good! :)

Of course the first question that pops up in your head is not about immutability, concurrency nor functional programming.

It is

How can I quit the Elixir shell?

Today this question will be answered.

Ctrl-C

When you start your iex sessions, you are greeted with:

Interactive Elixir (1.2.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

Ctrl-C actually puts you into the Break command. From there, you can exit the shell using (a)bort:

iex(1)>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a
george:~$

What I’m used to do is to hit Ctrl-C twice. It has the same effect as the abort command.

The Break command can be triggered from any running Elixir code and not only iex. But I always feel this is somewhat dirty. That by dropping into the Break command and exiting from there, I’m leaving my session opened. I know this is not the case but I went to find other ways to exit the shell.

Ctrl-G

You may have heard about Ctrl-G. If you type it in your IEx session, you’ll see:

iex(1)>
User switch command

This drops you into the User switch command, or Job Control Mode (JCL), if you read about it in the Erlang documentation.

In this mode, you can create new shells (local and remote ones), list and terminate them:

User switch command
 --> h
  c [nn]            - connect to job
  i [nn]            - interrupt job
  k [nn]            - kill job
  j                 - list all jobs
  s [shell]         - start local shell
  r [node [shell]]  - start remote shell
  q                 - quit erlang
  ? | h             - this message
 -->

If you use q in this mode, you’ll halt your Erlang system, similar to aborting through the Break command. However Job Control Mode only works within IEx and therefore it is somewhat more restricted compared to Ctrl+C.

Ctrl-\

You may have tried Ctrl-D, a.k.a the End-of-Transmission character. Turns out Erlang and Elixir don’t understand it the way we are used from other REPLs.

What I didn’t know is that you can exit the shell by sending Ctrl-\. The shell will exit immediately. As far as I know, it has the same effect as aborting the shell in the Break command, it doesn’t affect remote nodes and it also works outside of iex (for example, you can use to terminate your tests). I only found out about it in this brief passage in the Erlang FAQ.

Now that’s a quick and proper exit. My search is complete. Now I just need to retrain my muscle memory.

P.S.: This post was originally published on Plataformatec’s blog.