Is it possible to add color and other effects to the iex
prompt? Does iex
have a resource file (like .irbrc
for Ruby's irb
)? Is there a customization API that allows prompt customization (like Ruby's IRB.conf
)?
Yes, yes, and yes!
To customize your prompt, you'll need several things:
.iex.exs
file in your home directory. Create this file if it doesn't exist. It will be executed when iex
launches.Application.put_env
to enable ANSI. You may need this if iex
on your platform (e.g., Windows 10) does not detect ANSI support.IEx.configure
to enable color and set the prompt.IO.ANSI
does not currently expose all the cursor movement codes, but raw codes will work for terminals that support them.IO.ANSI
formatting functions.IO.ANSI.reset
to turn off any remaining formatting.IO.chardata_to_string
.Here's what works for me with iex
1.3.0 in Terminal and iTerm2 3.0.3 on OS X 10.11.5 and in Console, GitBash, and ConEmu on Windows 10:
# ~/.iex.exs
Application.put_env(:elixir, :ansi_enabled, true)
IEx.configure(
colors: [enabled: true],
default_prompt: [
"\e[G", # ANSI CHA, move cursor to column 1
:magenta,
"%prefix", # IEx prompt variable
">", # plain string
:reset
] |> IO.ANSI.format |> IO.chardata_to_string
)
This code works pretty well, but my prompt only takes effect after the first interaction: when iex
first launches, it shows its builtin prompt. If I hit return, then my prompt goes into effect. If anyone knows how to fix that, please share.
[UPDATED: modified to work better on Windows.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With