AndyL

AndyL

I’d like to add some custom helper functions to iex. I use .iex.exs and can define a module there with my custom functions. But I can’t figure out how to auto-import the module into my iex environment. I’ve tried hacking the iex bash script (at /usr/local/bin/iex), browsed thru the iex source - no luck. Is there a way to extend IEx.Helpers, or auto-import my .iex.exs module?

Showing Posts 20 to 11

axelson

axelson

Scenic Core Team

I knew that aliasing would work but I swear I tried an import_file_if_available ".iex.exs" and that it failed to actually import a module defined in that .iexs.exs but indeed it does work great! Thank you! :tada:

> cat ~/.iex.exs
defmodule JaxIEx do
  def rr(opts \\ []), do: IEx.Helpers.recompile(opts)
end

import JaxIEx
import_file_if_available ".iex.exs"
[07:04:23] jason@Jasons-MacBook-Air /Users/jason/dev/password-validator
> cat .iex.exs
defmodule ProjIExUtils do
  def my_helper, do: IO.puts("hello, world")
end

import ProjIExUtils
[07:04:41] jason@Jasons-MacBook-Air /Users/jason/dev/password-validator
> iex --dot-iex ~/.iex.exs -S mix
Erlang/OTP 26 [erts-14.2.5.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Interactive Elixir (1.17.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> rr
:noop
iex(2)> my_helper
hello, world
:ok
iex(3)>

Now if I update my i and s aliases to:

alias s='iex --dot-iex ~/.iex.exs -S mix phx.server'
alias i='iex --dot-iex ~/.iex.exs -S mix'

I can type rr instead of recompile :grin:

zachallaun

zachallaun

Looking at what import_file_if_available does, it’s just a macro that reads the file, parses it with Code.string_to_quoted and then spits it out. So I think it should make any aliases/imports available unless you’ve established a new scope. So just to confirm, this definitely doesn’t work when running with iex --dot-iex ~/.iex.exs?

# ~/.iex.exs

alias Example.One
alias Example.Two

import_file_if_available ".iex.exs"

Edit: I was just able to try this out and it does seem to be working for me, which is good news! So there must be something else going on, I think:

~/dev/lib_elixir main wip ⇡1                                                                                10:54:22 AM
❯ echo -n 'alias Example.One\nimport_file_if_available ".iex.exs"' > ~/.iex.exs

~/dev/lib_elixir main wip ⇡1                                                                                10:56:14 AM
❯ echo -n 'alias Example.Two' > .iex.exs

~/dev/lib_elixir main wip ⇡1 ?1                                                                             10:56:23 AM
❯ iex --dot-iex ~/.iex.exs
Erlang/OTP 27 [erts-15.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.17.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> One
Example.One
iex(2)> Two
Example.Two
axelson

axelson

Scenic Core Team

This has been a great development! However I’m still struggling with one specific aspect.

If a given project defines it’s own .iex.exs that imports a module, is there a way that I can also have a ~/.iex.exs that imports a module? So far I haven’t been able to get that to work without both .iex.exs scripts from having knowledge of each other.

Things I’ve tried:

  • I can use iex --dot-iex ~/.iex.exs to import my own .iex.exs, but then I don’t have a way to load the project-specific .iex.exs
    • If I load it with Code.eval_file(".iex.exs") then any imports it defines are not available
    • If I load it with import_file_if_available(".iex.exs") then any imports it defines is not available

The only workaround I have so far is for my ~/.iex.exs to have knowledge of the name of all modules that any of the project-specific .iex.exs wants to import and import them all if they exist.

josevalim

josevalim

Creator of Elixir

In Elixir v1.14+ you will be able to import the module as you define it in .iex.exs files, as we now evaluate it line by line: Evaluate --dot-iex line by line · elixir-lang/elixir@395b053 · GitHub

axelson

axelson

Scenic Core Team

Ah, ok. I’ll just define the helper somewhere else then.

Qqwy

Qqwy

TypeCheck Core Team

Unfortunately, no, because it is not possible to import a module in the same file as in which it was defined: you will get above compile error scenario.

axelson

axelson

Scenic Core Team

Is there an easy way to define an IEx helper now? I’d prefer to keep the helper definition inside .iex.exs if possible.

chazsconi

chazsconi

That works

Thanks @andy !

AndyL

AndyL OP

You need to start iex with the -pa option to append your home dir to the iex search path. Try iex -pa ~/

Also FYI you can use import_if_available - prevents the error message if the module is not found.

chazsconi

chazsconi

Doesn’t work for me unfortunately.

I have the Elixir.IEXHelper.beam file in my home folder and in
.iex.ex I have
import IEXHelper
I get
** (CompileError) /Users/chazsconi/.iex.exs:1: module IEXHelper is not loaded and could not be found

Any suggestions for how to fix this?

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews