Search code examples
libreadline

Prevent readline line submit


I'm using readline in an application that follows the input realtime. I don't want to allow the user to press Enter, Control + J, or any other equivalent, and thus unintentionally clearing the field.

I'm actually forwarding the keys myself, so I can catch and prevent certain key inputs, but I don't think this is the way to go because reverse search (Control - R) must also be handled.

I've looked over the gnu docs, but haven't found anything useful.

My code is similar to this https://github.com/ulfalizer/readline-and-ncurses


Solution

  • You can inhibit this keys configuring key bindings in the initialization file of libreadline :

    1. in ~/.inputrc to configure for current user
    2. in /etc/inputrc to configure for all users.

    To inhibit Enter and Control+J, you could add to the initialization file :

    RETURN:
    C-J:

    If you prefer you can do this programmatically using rl_unbind_key, adding to your code :

    rl_unbind_key('\r'); // unbind Enter
    rl_unbind_key('\n'); // unbind Control+J