How to quit the Elixir shell (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.


Subscribe to Elixir Radar

6 responses to “How to quit the Elixir shell (IEx)?”

  1. In Windows (Cygwin) this is not working.

  2. VinceP says:

    For Windows use iex –werl as then you get working tab completion and some other features in a full Erlang window. To quit that use Control + G then ‘q’uit. I would say don’t bother using iex in a normal command window as it’s too frustrating to be useful.

  3. billstclair says:

    :erlang.halt

  4. josevalim says:

    You can use `System.halt` which uses Elixir’s module. The trouble with such command though is that it will shut down the node you are currently connected to on remote cases instead of just exiting the shell.

  5. Onorio Catenacci says:

    I use iex in a regular command window all the time. In fact I find werl a bit clumsy. Different strokes, I guess.

  6. Andre Uratsuka Manoel says:

    I’m sure it works to get you out of iex. Ctrl- is usually mapped to the KILL signal, so it may not only end the session, but also generate a core dump.