5 Aha Tricks to Make Elixir‘s Interactive Shell more Pleasant

For the freshman of Elixir, there are some small tricks to make your programming way more pleasant and unhindered.

1 Shell History Enabled

We are familiar with the interactive shell of Elixir. In some cases, we need to restart it. It’s not like python or nodejs, the history of command will disappear after restarting. BUT……Fortunately, we can handle this problem!

Just add export ERL_AFLAGS="-kernel shell_history enabled" to the environment ( .bashrc, .zshrc, .envrc…… ) .

Pleasant!

2 Check the Type of Variables

In daily work , it’s eazy to check the type of an variable — just type:

i variable_u_want_check

Pleasant!

3 Fetch the Documents of Modules and Functions

This command is very helpful to know the using of modules and functions quickly:

h Module_Name/Func_Name

The module:

defmodule BlogWeb.Router do
@moduledoc """
router of blogweb!

http://ahasmarter.com
"""
end

The return of command:

The function:

@doc """
get article by article id!
"""
@spec get_article_by_id(integer()) :: Article.t()
def get_article_by_id(id) do
Article
|> where(id: ^id)
|> Repo.one()
end

The return of command:

This command is f**king awesome when I forget the using of a function or using a new library.

  • Tips: It’s helpful for you and the users of your code, if you writing the annotations, specs and some links about this func! :)

Pleasant!

4 Recompile Force

Sometimes we start one more iex for one project. In this scene, when we upgrading the code in iex, compile() without parameter will only work on the first iex. The solution is simple……just add a parameter to the compile!

recompile([force: true])

Pleasant!

5 Infinity Output

We will be annoyed when we trying to output something to console and the result is elliptic!

The Savior is|> IO.inspect( limit: :infinity)!

6 Extra Trick! Tips for compile Error!

Sometimes when we upgrading libraries, the project will encounter “compile error”. The first thing we should do is to clean deps and recompile it. There is a 99% chance that problem will be solved.

mix deps.clean --all
mix deps.get
mix compile

You should check the root dir of project, if there is a folder named “.elixir_ls”, you need to delete the builder folder in it and wait a while, your problem will be solved automatically!

And……If you using Vs-code or some other IDE, maybe facing a situation: the code is compiled well in terminal but failed in IDE.

Pleasant!