Recognizing more `ctrl-` keys in Iex

26 views
Skip to first unread message

Robb Shecter

unread,
Oct 13, 2019, 12:43:49 AM10/13/19
to elixir-lang-core
I really miss `ctrl-l` to clear the screen and `ctrl-d` to exit like other REPLs provide. Since `ctrl-d` is ignored, I would chose to alias it to one of the two "break" codes: `ctrl-c` or `ctrl-g`.

I've read through the [Iex source code](https://github.com/elixir-lang/elixir/tree/master/lib/iex/lib) and don't see how to extend the code. The closest thing I've found is [the `tab` completion handler](https://github.com/elixir-lang/elixir/blob/master/lib/iex/lib/iex/autocomplete.ex). It seems to work via a plug-in strategy as this section implies:

  @doc """
  Provides one helper function that is injected into connecting
  remote nodes to properly handle autocompletion.
  """

 
def remsh(node) do
    fn e
->
     
case :rpc.call(node, IEx.Autocomplete, :expand, [e]) do
       
{:badrpc, _} -> {:no, '', []}
        r
-> r
     
end
   
end
 
end


But I still can't figure out how the `tab` character is specified for this "hook" (if it even is a hook).

Are "special" characters like `tab` and `ctrl-c` handled exclusively by an Erlang server, and somehow Iex's current architecture rules out adding new key combinations?

José Valim

unread,
Oct 13, 2019, 2:20:15 AM10/13/19
to elixir-l...@googlegroups.com
Yes, they would need to be contributed to the Erlang server. :)
--


José Valim
Skype: jv.ptec
Founder and Director of R&D

Robb Shecter

unread,
Oct 13, 2019, 6:08:52 PM10/13/19
to elixir-lang-core
José Valim wrote:
Yes, they would need to be contributed to the Erlang server. :)

Thanks! I suspected that. So every keystroke first handled by the Erlang server, and then sent back to the iex code in special cases? (tab, ctrl-c, -g, -\, and maybe a couple of others)

Do you have any ideas for how a hack might be possible ... to trap certain keystrokes and handle them?

My current best idea is to just use an app like Autokey and assign macros to keystrokes while using iex.

Robb Shecter

unread,
Oct 13, 2019, 6:15:17 PM10/13/19
to elixir-lang-core
By the way, which Erlang project/repo would I propose these changes to?

I couldn't figure it out from the Iex source.

José Valim

unread,
Oct 13, 2019, 7:24:10 PM10/13/19
to elixir-l...@googlegroups.com
There is a callback for auto completion but most things are not even forwarded to IEx. The code, IIRC, is in Erlang’s stdlib app (search around user_drv, edlin, etc).

Robb Shecter

unread,
Oct 14, 2019, 12:38:32 AM10/14/19
to elixir-lang-core
José Valim wrote:
There is a callback for auto completion but most things are not even forwarded to IEx. The code, IIRC, is in Erlang’s stdlib app (search around user_drv, edlin, etc)

Thanks! Will do. 

Robb Shecter

unread,
Oct 14, 2019, 12:47:54 AM10/14/19
to elixir-lang-core
Interesting - edlin.erl is implementing some emacs functions:


And ctrl-d actually has a function, which I didn't realize - as in emacs, to delete the character ahead of the cursor:

Jonathan Arnett

unread,
Oct 14, 2019, 3:58:39 PM10/14/19
to elixir-lang-core
For what it's worth, bash also uses Emacs-style line editing. The difference between bash and erl is that bash will exit when there's nothing after the prompt and ctrl-d is received. When there is something after the prompt, bash will either delete the following character, if it exists, or ring the bell.

As an emacs user, I recognize all the oddities of various forms of emacs emulation 🙂

Something I'll lob into this conversation as well is that erl has a `q()` function that exits the shell. I'd love if IEx had a `q()` or `exit()` or something of that nature.

Robb Shecter

unread,
Oct 14, 2019, 10:15:00 PM10/14/19
to elixir-lang-core
Interesting! So maybe a case could be made to the Erlang maintainers that the emacs-like char handling is a little broken and can be improved to match e.g. bash's. 

The other option I've found is to run iex from rlwrap. It should be configurable to capture ctrl- keypresses (like Ctrl-L), and send a text string (like clear) up to iex.

Robb Shecter

unread,
Oct 14, 2019, 10:40:58 PM10/14/19
to elixir-lang-core
I created an Issue, asking if they'd be open to a PR: https://bugs.erlang.org/browse/ERL-1068
Reply all
Reply to author
Forward
0 new messages