Elixir - IEx


Erlang shell
Eshell (erl)
Elixir shell
IEx (iex)
UNIX shell
Bash, etc.

  1. https://hexdocs.pm/iex/IEx.html
  2. https://hexdocs.pm/iex/IEx.Helpers.html
  3. https://stackoverflow.com/documentation/elixir/1283/iex-console-tips-tricks (closed)
  4. http://echobind.com/blog/2017-08-31-tips-and-tricks-for-iex/

.iex.exs

IEx loads the 1st file it finds:

=> each project can have its own .iex.exs with local aliases that don’t pollute global namespace.

=> IEx configs are not merged: if current project has its own .iex.exs, user-wide config ~/.iex.exs is not read.

aliases

contents of .iex.exs is evaluated in shell’s context so it can be used to alias popular modules to cut down on typing:

alias Neko.{Achievement, UserRate}

or else try to use QuickAlias package (though it wasn’t working for me when I tried it).

quit IEx

  1. http://blog.plataformatec.com.br/2016/03/how-to-quit-the-elixir-shell-iex/

only graceful ways to quit IEx save shell history (when using erlang-history).

UPDATE (2017-07-28)

since Erlang/OTP 20 shell history is supported out of the box - if using it instead of erlang-history patch all ways to quit IEx allow to save shell history except for the last one (<C-\>).

shell history

http://nithinbekal.com/posts/elixir-shell-history/:

$ git clone https://github.com/ferd/erlang-history.git
$ cd erlang-history
$ sudo make install

shell history since from now is stored in ~/.erlang-hist.nonode@nohost (it’s a binary file - not plain text).

UPDATE: shell history is now stored in ~/.erlang-history/ - you might need to remove ~/.erlang-hist.nonode@nohost for it to work.

for shell history to be saved quit IEx gracefully - using either <C-c>a<CR> or <C-g>q<CR> commands.

erlang-history must be compiled for each new version of Erlang/OTP kernel:

UPDATE (2017-07-28)

https://hexdocs.pm/iex/IEx.html#module-shell-history:

From Erlang/OTP 20, it is possible to get shell history by passing some flags that enable it in the VM.

https://github.com/ferd/erlang-history:

Since Erlang/OTP-20rc2, Shell history is supported out of the box (although initially disabled by default) through a port of this library to the Erlang/OTP code base. Enable the shell in these versions by setting the shell_history kernel environment variable to enabled with export ERL_AFLAGS=”-kernel shell_history enabled” added to your environment variables.

~/.zshenv:

export ERL_AFLAGS="-kernel shell_history enabled"

using erlang-history is no longer required (though I haven’t found an easy way to uninstall it).

get result of last evaluated expression (= _ in pry)

  1. https://hexdocs.pm/iex/IEx.Helpers.html#v/1
iex> 123
123
iex> v()
123

optional argument can be passed to return nth expression in current IEx session: positive number indicates absolute position and negative number indicates relative one (say, v() == v(-1)).

cancel multiline command

  1. https://stackoverflow.com/questions/27591417
  2. https://hexdocs.pm/iex/1.0.5/IEx.html
iex> foo =
...> #iex:break
  1. http://readline.kablamo.org/emacs.html

recompile current Mix application (= reload! in pry)

  1. http://stackoverflow.com/a/36494891/3632318
iex> recompile()

also it’s possible to recompile specific module:

iex> r(Foo.Bar)

suppress long output (= ; in pry)

  1. http://stackoverflow.com/a/39208906/3632318

add another expression at the end of the line after semicolon:

iex> Foo.bar(); 0
0

don’t truncate long lists or strings

  1. https://stackoverflow.com/questions/29566248/elixir-io-inspect-to-not-trim-a-long-list
  2. https://hexdocs.pm/elixir/Inspect.Opts.html

for collections (defaults to 50 items):

iex> IO.inspect(list, limit: :infinity)

for strings and charlists (defaults to 4096 bytes):

iex> IO.inspect(list, printable_limit: :infinity)

force display a list of integers

  1. https://github.com/elixir-lang/elixir/wiki/FAQ#4-why-is-my-list-of-integers-printed-as-a-string
  2. https://www.theguild.nl/print-list-of-integers-as-integers-in-iex/

see also Elixir post (character lists section) for ways to print charlist as a collection of codepoints.

set charlists option of Kernel.inspect/2

  1. https://hexdocs.pm/elixir/Inspect.Opts.html

NOTE: Kernel.inspect/2 always returns a string.

# in fact any value passed to `charlists` option except
# for `as_charlists` acts as `as_lists`:
iex> inspect([27, 35, 51], charlists: :as_lists)
"[27, 35, 51]"
iex> inspect([27, 35, 51], charlists: :as_charlists)
"'\\e#3'"

set charlists inspect option used by IEx

  1. https://hexdocs.pm/iex/IEx.html#configure/1-inspect
iex> IEx.configure(inspect: [charlists: :as_lists])
iex> [27, 35, 51]
[27, 35, 51]

or else it can be set in IEx config file:

# ~/.iex.exs

IEx.configure(inspect: [charlists: :as_lists])

this option will be respected by IO.inspect/2 and by the shell when printing results of expression evaluation - but not by Kernel.inspect/2.

add any non-printable character

0 is usually added:

iex> [27, 35, 51] ++ [0]
[27, 35, 51, 0]