How to do CLI Tab Completion?

I want to write a CLI that does tab completion. For a comparable I studied IEx.Server.

It looks to me like this code takes input using IO.gets (on line 336). How does it trigger on the tab character? I can’t figure it out!

I think IO.gets traps the tab character when running under iex but not when running in elixir. Could that be right? Here’s a script test.exs that I used for testing:

#!/usr/bin/env elixir

IO.puts """
Try running this test two ways:
  > elixir text.exs
  > iex test.exs

`IO.gets` handles tab characters differently under elixir vs iex.  WHY??
"""

IO.gets("input prompt > ") |> IO.inspect(label: "RESULT")

How to write a CLI that has tab completion? Any tips appreciated!!

3 Likes

@AndyL Were you ever able to get this to work? I’m trying to do something similar.

I know that IEx uses :io.setopts with the expand_fun option to get tab completion, but I haven’t been able to get my custom expand function to fire.

My best guess is that some kind of integration with the erlang shell is required, but I’m not sure what parts of the IEx codebase are relevant to this.

1 Like

Were you ever able to get this to work?

No!

OTP 26 introduced a new shell, but I haven’t yet seen a working solution.

There’s a long-running discussion on the Owl repo which describes different experiments that people have tried.