When using the Elixir REPL iex
there’s a bunch of tricks that can personalize and make your experience easier. Your best resource is of course the documentation it self.
Here are some of my favourites:
Configuring auto imports
You can create an .iex.exs that will be executed when you start iex in the same directory that the file resides in. In that file you can import other files and alias modules that you don’t want to have to alias all the time, ecto schemas come to mind.
# .iex.exs
alias Figueroa.Blog.Post
alias Figueroa.Thoughts.Thought
alias Figueroa.Repo
import Ecto.Query
# helper queries
tq = from p in Post, where: p.published == true
...
Use the history
Keeping the history between sessions is a killer feature IMO and is easily enabled by setting:
export ERL_AFLAGS="-kernel shell_history enabled"
See here for more information.
And talking about history, how often haven’t you retyped something, after accidentally committing it to the repl, that you wanted to map or reduce? Just to either re-type it or pressing arrow up?
Checkout the v()
method! It takes the last result and makes it available:
iex(3)> "Foo"
"Foo"
iex(4)> |> String.upcase()
"FOO"
What IEx does for you under the hood is to apply the v()
(actually v(-1)
) method befor the pipe-operator. v() |> String.upcase()
Some cosmetics
Also, just for the fun of it, you can configure the prompt as well, making it a bit personalized:
# .iex.exs
IEx.configure(
default_prompt: "figueroa_web(%counter)>",
auto_reload: true
)
Finally, noticed that last thing? auto_reload: true
with this lite configuration you don’t have to call recompile
in IEx anymore.