IEx v1.1.1 IEx.Helpers

Welcome to Interactive Elixir. You are currently seeing the documentation for the module IEx.Helpers which provides many helpers to make Elixir’s shell more joyful to work with.

This message was triggered by invoking the helper h(), usually referred to as h/0 (since it expects 0 arguments).

There are many other helpers available:

  • b/1 - prints callbacks info and docs for a given module
  • c/2 - compiles a file at the given path
  • cd/1 - changes the current directory
  • clear/0 - clears the screen
  • flush/0 - flushes all messages sent to the shell
  • h/0 - prints this help message
  • h/1 - prints help for the given module, function or macro
  • import_file/1 - evaluates the given file in the shell’s context
  • l/1 - loads the given module’s beam code
  • ls/0 - lists the contents of the current directory
  • ls/1 - lists the contents of the specified directory
  • pid/3 - creates a PID with the 3 integer arguments passed
  • pwd/0 - prints the current working directory
  • r/1 - recompiles and reloads the given module
  • recompile/0 - recompiles the current Mix project (requires iex -S mix)
  • respawn/0 - respawns a new IEx shell
  • s/1 - prints spec information
  • t/1 - prints type information
  • v/0 - retrieves the last value from the history
  • v/1 - retrieves the nth value from the history

Help for functions in this module can be consulted directly from the command line, as an example, try:

h(c/2)

You can also retrieve the documentation for any module or function. Try these:

h(Enum)
h(Enum.reverse/1)

To discover all available functions for a module, type the module name followed by a dot, then press tab to trigger autocomplete. For example:

Enum.

To learn more about IEx as a whole, just type h(IEx).

Summary

Functions

Compiles the given files

Changes the current working directory to the given path

Clears the console screen

Flushes all messages sent to the shell and prints them out

h()

Prints the documentation for IEx.Helpers

Loads the given module’s beam code (and ensures any previous old version was properly purged before)

Produces a simple list of a directory’s contents

Creates a PID with 3 non negative integers passed as arguments to the function

Prints the current working directory

Recompiles and reloads the given module

Recompiles the current Mix application

Respawns the current shell by starting a new shell process

Retrieves the nth expression’s value from the history

Macros

Prints the documentation for the given callback function

Prints the documentation for the given module or for the given function/arity pair

Evaluates the contents of the file at path as if it were directly typed into the shell

Prints the specs for the given module or for the given function/arity pair

Prints the types for the given module or for the given function/arity pair

Functions

c(files, path \\ ".")

Compiles the given files.

It expects a list of files to compile and an optional path to write the compiled code to (defaults to the current directory). When compiling one file, there is no need to wrap it in a list.

It returns the name of the compiled modules.

If you want to recompile an existing module, check r/1 instead.

Examples

c ["foo.ex", "bar.ex"], "ebin"
#=> [Foo, Bar]

c "baz.ex"
#=> [Baz]
cd(directory)

Changes the current working directory to the given path.

clear()

Clears the console screen.

This function only works if ANSI escape codes are enabled on the shell, which means this function is by default unavailable on Windows machines.

flush()

Flushes all messages sent to the shell and prints them out.

h()

Prints the documentation for IEx.Helpers.

l(module)

Loads the given module’s beam code (and ensures any previous old version was properly purged before).

This function is useful when you know the bytecode for module has been updated in the filesystem and you want to tell the VM to load it.

ls(path \\ ".")

Produces a simple list of a directory’s contents.

If path points to a file, prints its full path.

pid(x, y, z)

Creates a PID with 3 non negative integers passed as arguments to the function.

Examples

iex> pid(0, 21, 32)
#PID<0.21.32>
iex> pid(0, 64, 2048)
#PID<0.64.2048>
pwd()

Prints the current working directory.

r(module)

Recompiles and reloads the given module.

Please note that all the modules defined in the same file as module are recompiled and reloaded.

In-memory reloading

When we reload the module in IEx, we recompile the module source code, updating its contents in memory. The original .beam file in disk, probably the one where the first definition of the module came from, does not change at all.

Since typespecs and docs are loaded from the .beam file (they are not loaded in memory with the module because there is no need for them to be in memory), they are not reloaded when you reload the module.

recompile()

Recompiles the current Mix application.

This helper only works when IEx is started with a Mix project, for example, iex -S mix. Before compiling the code, it will stop the current application, and start it again afterwards. Stopping applications are required so processes in the supervision tree won’t crash when code is upgraded multiple times without going through the proper hot-code swapping mechanism.

Changes to mix.exs or configuration files won’t be picked up by this helper, only changes to sources. Restarting the shell and Mix is required in such cases.

If you want to reload a single module, consider using r ModuleName instead.

NOTE: This feature is experimental and may be removed in upcoming releases.

respawn()

Respawns the current shell by starting a new shell process.

Returns true if it worked.

v(n \\ -1)

Retrieves the nth expression’s value from the history.

Use negative values to lookup expression values relative to the current one. For instance, v(-1) returns the result of the last evaluated expression.

Macros

b(term)

Prints the documentation for the given callback function.

It also accepts single module argument to list all available behaviour callbacks.

Examples

b(Mix.Task.run/1)
b(Mix.Task.run)
b(Dict)
h(term)

Prints the documentation for the given module or for the given function/arity pair.

Examples

h(Enum)
#=> Prints documentation for Enum

It also accepts functions in the format fun/arity and module.fun/arity, for example:

h receive/1
h Enum.all?/2
h Enum.all?
import_file(path, opts \\ [])

Evaluates the contents of the file at path as if it were directly typed into the shell.

path has to be a literal string. path is automatically expanded via Path.expand/1.

Non-existent files

By default, import_file/1 fails when the given file does not exist. However, since this macro is expanded at compile-time, it’s not possible to conditionally import a file since the macro is always expanded:

# This raises a File.Error if ~/.iex.exs doesn't exist.
if ("~/.iex.exs" |> Path.expand |> File.exists?) do
  import_file "~/.iex.exs"
end

This is why an :optional option can be passed to import_file/1. The default value of this option is false, meaning that an exception will be raised if the given file is missing. If :optional is set to true, missing files will be ignored and import_file/1 will just compile to nil.

Examples

# ~/file.exs
value = 13

# in the shell
iex(1)> import_file "~/file.exs"
13
iex(2)> value
13
iex(3)> import_file "nonexisting.file.ex", optional: true
nil
s(term)

Prints the specs for the given module or for the given function/arity pair.

Examples

s(Enum)
s(Enum.all?)
s(Enum.all?/2)
s(is_atom)
s(is_atom/1)
t(term)

Prints the types for the given module or for the given function/arity pair.

Examples

t(Enum)
t(Enum.t/0)
t(Enum.t)