DEV Community

Mark
Mark

Posted on • Originally published at alchemist.camp

The .iex.exs file

The .iex.exs file is a "must have" convenience.

You can use it to set aliases, set imports and add define helper functions for every IEx session you run!

Here's what's in my global one:

import_if_available(Ecto.Query)
import_if_available(Ecto.Changeset)

defmodule AC do
  def update(schema, changes) do
    schema
    |> Ecto.Changeset.change(changes)
    |> Repo.update()
  end

  IEx.configure(
    colors: [
      syntax_colors: [
        number: :light_yellow,
        atom: :light_cyan,
        string: :light_black,
        boolean: [:light_blue],
        nil: [:magenta, :bright]
      ],
      ls_directory: :cyan,
      ls_device: :yellow,
      doc_code: :green,
      doc_inline_code: :magenta,
      doc_headings: [:cyan, :underline],
      doc_title: [:cyan, :bright, :underline]
    ],
    default_prompt:
      [
        # ANSI CHA, move cursor to column 1
        "\e[G",
        :light_magenta,
        # plain string
        "🧪 iex",
        ">",
        :white,
        :reset
      ]
      |> IO.ANSI.format()
      |> IO.chardata_to_string()
  )
end

And in this project, my local .iex.exs has:

alias Campsite.{Accounts, Content, Extract, Game, Repo}
alias Accounts.{User, Credential}
alias Content.{Article, Comment, Episode, Podcast}
alias Game.Request

With these in place, every time IEx starts up, commonly used modules are automatically aliased, Ecto queries are easier and there's a nice 🧪 prompt making it clear the terminal is in IEx. 🎉

Request a free email-based Elixir course from Alchemist.Camp

Discussion (0)