One of the less known (but useful) features of Elixir’s IEx module is the
open/1
function. Open works with either a given Module
,
Module.function/arity
or {file, line_number}
. Once configured, it allows the
developers to open a file with a given file path and line number. This is
incredibly useful while debugging inside a IEx.pry
session as it allows you
to jump to the place where a particular function is defined.
For example:
iex> open String.to_atom/1
Calling open
as above will open the file corresponding to the String
module
where to_atom
is defined.
To accomplish this IEx.Helpers
module uses the environment variable,
ELIXIR_EDITOR
. If the environment variable isn’t exported, it uses EDITOR
as a fallback.
However, by default it attempts to open the editor while keeping the iex
session alive. So, this command doesn’t work with vim
or nvim
right off
the bat.
Configuring open/1
to work with Vim (and Neovim)
I will be configuring open/1
to work with nvim
(my primary text editor).
But, a similar configuration can be used for vim
and other terminal-based
editors.
Let’s start by adding exporting the ELIXIR_EDITOR
environment variable.
# in ~/.bashrc (or ~/.zshrc)
export ELIXIR_EDITOR="nvim"
But this won’t work. In order for open/1
to work with nvim
, we need to
satisfy two cases:
open/1
should attempt to opennvim
in a separate window. This will keep theIEx
session alive while giving us the ability to open a file.open/1
attempts to open a file usingfile:line
notation, which doesn’t work withnvim
. So, we will have to configure the format of that notation.
For the first case, we can just open our nvim
in a new terminal as follows:
Linux Users (with gnome-terminal)
# in ~/.bashrc (or ~/.zshrc)
export ELIXIR_EDITOR="gnome-terminal -- nvim"
Mac Users
# in ~/.bashrc (or ~/.zshrc)
export ELIXIR_EDITOR="open -a Terminal 'nvim'"
This should allow us to call open, but we still have to configure the editor
command to take a line_number
. Elixir provides interpolation for __FILE__
and __LINE__
.
We can use them to open a file with a specific line_number
in
a Neovim window as follows:
Linux Users (with gnome-terminal)
# in ~/.bashrc (or ~/.zshrc)
export ELIXIR_EDITOR="gnome-terminal -- nvim +__LINE__ __FILE__"
Mac Users
# in ~/.bashrc (or ~/.zshrc)
export ELIXIR_EDITOR="open -a Terminal 'nvim +__LINE__ __FILE__'"
Now running and iex
and calling open/1
should open a file with a specific
line number.
With Tmux
Since I use tmux
, I changed my final version of the ELIXIR_EDITOR
env to
look like this:
Linux Users (with gnome-terminal)
# in ~/.bashrc (or ~/.zshrc)
export ELIXIR_EDITOR="gnome-terminal tmux new -- nvim +__LINE__ __FILE__"
Mac Users
# in ~/.bashrc (or ~/.zshrc)
export ELIXIR_EDITOR="open -a Terminal 'tmux new nvim +__LINE__ __FILE__'"
This allows me to open
a file in nvim
with tmux configurations.
Here is the finished product:
This also works with open/0
which opens the current prying location inside
a prying session started by either calling IEx.pry/0
or IEx.break!/4
.
Check out IEx.Helpers.open/1
for more information.