99

When I use IEx and make a typo like additional bracket or ", then most of the time I get a syntax error. But there are cases like this one:

iex(3)> Enum.each '12345', &(IO.puts(&1"))    
...(3)> end   
...(3)> )   
...(3)> '    
...(3)> end    
...(3)> ""    
...(3)> ... ? How to produce syntax error ?    
...(3)>     
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded    
      (v)ersion (k)ill (D)b-tables (d)istribution   

I cannot make a syntax error and continue from scratch and I have to relaunch entire IEx. Is there any keyboard shortcut or command to skip performing current iex(3) and go to next iex(4)?

4d2025
  • 1,013
  • 1
  • 7
  • 5
  • 3
    the solutions to this are pretty annoying, I wished they included an option in the `BREAK` menu to cancel current multiline command, or make the first ctrl-c cancel it... – Guido Tarsia Oct 20 '19 at 22:30

3 Answers3

123

Start your line with #iex:break

iex(1)> Enum.each '12345', &(IO.puts(&1"))    
...(1)> ...
...(1)> #iex:break

** (TokenMissingError) iex:1: incomplete expression
sasajuric
  • 5,949
  • 1
  • 22
  • 18
  • 4
    This is mentioned in the documentation, in the section "Expressions in IEx" (before the functions documentation begins): http://elixir-lang.org/docs/v1.0/iex/IEx.html – alxndr May 29 '15 at 18:50
  • and how does one interrupt a long running command and cancel it? – Thomas Browne Aug 23 '16 at 20:21
  • Oh dear.. reaching for text expander again... https://smilesoftware.com/textexpander shall abbreviate that to `;iexb` – arcseldon Oct 03 '17 at 13:42
75

In general, you can also press Ctrl + G, which will drop you into to the "User switch command" mode with a ---> prompt. From here, you can type i to interrupt your IEx session, then c to reconnect to it; IEx will then state ** (EXIT) interrupted and return to the state it was in immediately before you got stuck. This is a feature of Erlang's shell (erl) that was inherited by IEx.

Example of this in action:

iex(3)> Enum.each '12345', &(IO.puts(&1"))
...(3)> end
...(3)> )
...(3)> '
...(3)> end
...(3)> ""
...(3)>        <-- Ctrl-G goes here (not shown normally)
User switch command
 --> i
 --> c
** (EXIT) interrupted
iex(3)> 
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
YellowApple
  • 851
  • 5
  • 3
10

My quick and dirty solution is to produce a syntax error with by spamming """"""""" (which will be interpreted as heredoc).

In your example:

iex(3)> Enum.each '12345', &(IO.puts(&1"))
...(3)> end
...(3)> '
...(3)> end
...(3)> ""
...(3)> """"""""""""""""""""
** (SyntaxError) iex:8: heredoc allows only zero or more whitespace characters followed by a new line after """
pzmarzly
  • 778
  • 15
  • 29