Want to learn?

Running Elixir

Interactive shell

Running shell

Once you're up and running you can run Elixir interactive IEx shell:

➜ iex
Erlang/OTP 22 [DEVELOPMENT] [erts-10.0.3] [source-0e9c319480] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

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

Shell is most of the time used for testing things out. When you want to check a piece of code, it's much faster to run it inside IEx rather than create a separate file and execute it.

Helpers

IEx comes bundled with a bunch of helpers. You can access them by typing h() in the shell.

iex> h()

                                  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).

You can use the h/1 function to invoke the documentation for any Elixir module
or function:

    iex> h(Enum)
    iex> h(Enum.map)
    iex> h(Enum.reverse/1)

You can also use the i/1 function to introspect any value you have in the
shell:

    iex> i("hello")

There are many other helpers available, here are some examples:

  • b/1 - prints callbacks info and docs for a given module
  • c/1 - compiles a file into the current directory
  • c/2 - compiles a file to the given path
  • cd/1 - changes the current directory
  • clear/0 - clears the screen
  • exports/1 - shows all exports (functions + macros) in a module
  • 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
  • i/0 - prints information about the last value
  • i/1 - prints information about the given term
  • ls/0 - lists the contents of the current directory
  • ls/1 - lists the contents of the specified directory
  • open/1 - opens the source for the given module or function in
    your editor
  • pid/1 - creates a PID from a string
  • pid/3 - creates a PID with the 3 integer arguments passed
  • ref/1 - creates a Reference from a string
  • ref/4 - creates a Reference with the 4 integer arguments
    passed
  • pwd/0 - prints the current working directory
  • r/1 - recompiles the given module's source file
  • recompile/0 - recompiles the current project
  • runtime_info/0 - prints runtime info (versions, memory usage, stats)
  • v/0 - retrieves the last value from the history
  • v/1 - retrieves the nth value from the history

Help for all of those functions can be consulted directly from the command line
using the h/1 helper itself. Try:

    iex> h(v/0)

To list all IEx helpers available, which is effectively all exports (functions
and macros) in the IEx.Helpers module:

    iex> exports(IEx.Helpers)

This module also includes helpers for debugging purposes, see IEx.break!/4 for
more information.

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

As you can see there are various of options that you can execute with h. For example, when you want to check what's inside of Enum module (which you'll use quite often) you can simply type:

iex> h(Enum)

                                      Enum

Provides a set of algorithms that enumerate over enumerables according to the
Enumerable protocol.

    iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end)
    [2, 4, 6]

Some particular types, like maps, yield a specific format on enumeration. For
example, the argument is always a {key, value} tuple for maps:

    iex> map = %{a: 1, b: 2}
    iex> Enum.map(map, fn {k, v} -> {k, v * 2} end)
    [a: 2, b: 4]

Note that the functions in the Enum module are eager: they always start the
enumeration of the given enumerable. The Stream module allows lazy enumeration
of enumerables and provides infinite streams.

Since the majority of the functions in Enum enumerate the whole enumerable and
return a list as result, infinite streams need to be carefully used with such
functions, as they can potentially run forever. For example:

    Enum.each Stream.cycle([1, 2, 3]), &IO.puts(&1)

You can take advantage of helpers as a tool for daily programming support.

Autocomplete

Sometimes you might forget naming of functions, etc. There is a tool that can help you with that. For instance, you can type the beginning of function name and then press tab button to use built-in autocomplete tool.

iex> Enum.s
scan/2 scan/3 shuffle/1 slice/2
slice/3 sort/1 sort/2 sort_by/2
sort_by/3 split/2 split_while/2 split_with/2
sum/1

Escaping shell

Once you're done playing around with IEx, you might want to escape it. You can do it by:

  • typing Ctrl + c twice,

  • typing Ctrl + G followed by q and Return,

  • some systems support also Ctrl + \.

Executing code from a file

If you want to run Elixir code from a file you can do that by compiling and running .ex or .exs file. The second file extension indicates that we're dealing with interpreted code.

Here is how we can execute those files:

  1. The content of test.ex/test.exs:

    IO.puts 'Hello world'
    
  2. In terminal:

    ➜ elixir test.ex
    Hello world
    
    or
    
    ➜ elixir test.exs
    Hello world
    

Both .ex and .exs are compiled when running elixir command. The only difference is that .ex will generate .beam file as a result of compilation. Such a file contains the bytecode. The result of .exs compilation is loaded into memory rather than a file.

Check our latest product - it's based on our experience of managing over 50-people strong company. The tool we're missing as a small company and not an enterprise.

humadroid.io is an employee and performance management software. It's an unique tool allowing everyone to be in the loop - by having up to date info about co-workers, time-off, benefits, assets, helping with one-on-ones, being a go-to place for company-wide announcements.

Check out humadroid.io
Top

Contact us

* Required fields

The controller of your personal data provided via this contact form is Prograils sp. z o.o., with a registered seat at Sczanieckiej 9A/10, 60-215 Poznań. Your personal data will be processed in order to respond to your inquiries and for our marketing purposes (e.g. when you ask us for our post-development, maintenance or ad hoc engagements for your app). You have the rights to: access your personal data, rectify or erase your personal data, restrict the processing of your personal data, data portability and to object to the processing of your personal data. Learn more.

Notice

We do not track you online. We use only session cookies and anonymous identifiers for the purposes specified in the cookie policy. No third-party trackers.

I understand
Elo Mordo!Elo Mordo!